GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous classes defined in a method and final var Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous classes defined in a method and final var" Watch "Anonymous classes defined in a method and final var" New topic
Author

Anonymous classes defined in a method and final var

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

The text books that I have read so far did not mention this restriction.
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
yes it is write. Anonymous class can access only final variables from enclosing method.
Here anonymous class extends thread class which is implementing run() method.
Vicky Jain
Ranch Hand

Joined: Aug 02, 2003
Posts: 52
Hi Barkat Mardhani,
In the code you are creating the method-local inner class which is the subclass of Thread. Also, method-local inner class can't access the local variables unless they are made final. This is perfectly mentioned in the chapter 8 of K&B book.


SCJP1.4(96%)<br />A drop of ink can make millions think.
chang Anne
Greenhorn

Joined: Jul 02, 2003
Posts: 29
Hello,
method-local inner classes can't access local variables inside Outer Class's method, ulness local variables are marked final.
I know a variable is marked final means can't change the value.
But why, method-local inner classes can access local variables are marked final?
Thank you.
chang Anne
Greenhorn

Joined: Jul 02, 2003
Posts: 29
Hello,
method-local inner classes can't access local variables inside Outer Class's method, ulness local variables are marked final.
I know a variable is marked final means can't change the value.
But why, method-local inner classes can access local variables are marked final?
Thank you.
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
K&B give a good first half of an explanation of why the local variables must be declared final. They point out that the local/anonymous class may have a life beyond the existence of the variables on the stack. They don't say how java solves this problem. My understanding is that these classes DON'T operate on these variables. Instead, they create and hold COPIES of them. In order to guarantee that the variables and the copies don't get out of sync, java insists the variables be final.
Do I have that right?


The Inner that is named is not the true Inner.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Steve,
I guess my understanding same as you stated.
The reason I started this thread was that in K&B book they talk about (non-anonymous) method-local class.
I came accross with a mock exam (Val's a very good and tough one) with a question where anonymous method-local class was trying to access method variable. I thought it is worth bringing up here because book does not mention that.
Thanks
Barkat
 
GeeCON Prague 2014
 
subject: Anonymous classes defined in a method and final var