The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
March 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

instance initialization vs. field initialization in anonymous inner classes

Posted by Andrew L. Marshall on March 03, 2001 at 2:32 PM

On page 376 of Thinking In Java, 2nd edition, Bruce Eckel gives an example of creating a "constructor" within an anonymous inner class using instance initialization. Now, I do understand what instance initialization is, and I do know what a field variable is, and I do readily concede that Bruce's example code Parcel9.java will not compile without instance initialization.

But I do not understand why instance initialization *must* be used. Why is it that the compiler balks at the "if(cost > 100)" conditional, when, experimentally, I initialize the "cost" variable as a field variable with the value "Math.round(price)", and I do not include the instance-initializing brackets around the conditional?

What's wrong with having a conditional that evaluates a field variable from within an anonymous inner class?

I'm hoping someone will explain this example to me; I'm obviously missing something pretty basic!! Thanks in advance, and I will really appreciate any help people are kind enough to give me...

-Andrew




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us