aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why local inner classes CAN 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 "Why local inner classes CAN "see" final variables?" Watch "Why local inner classes CAN "see" final variables?" New topic
Author

Why local inner classes CAN "see" final variables?

Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
Hi all, i'm wondering why a local inner class, that is, an inner class defined inside a method, can "see" a final variable declared within the method body? I know it can't see non-final variables because of scope issues, but why it's different with the final keyword?



Thanks again!

Leonardo.


Leonardo Crespo
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
This is what i feel.
Method local inner classes can only be instanciated in the method in which it is declared. When a call to the method completes the local varibles that belongs to the method are lost, but since the inner class may be alive so it can access only the final variables & members


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
final variable are also local i dont think u can access it once method is popped from stack.

correct me if reqd.
Natta Pon
Greenhorn

Joined: May 19, 2004
Posts: 5
Because final variable will be init. when the class is first loaded, and still be there all the time.
So, inner class can see this final variable even method is finished.


SCJP 1.4
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
Elaborate, your comment does not make sense to me.
Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
I'm not sure i got it. The reason method-local variables are not available to method-local inner classes is that they go out of scope when the method completes.

What's in the "final" keyword that makes those variables persist? I feel that method-local final variables should go out of scope too, but that's not the case.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Anonymous inner classes require final variables because of the way they are implemented in Java. An anonymous inner class (AIC) uses local variables by creating a private instance field which holds a copy of the value of the local variable. The inner class isn't actually using the local variable, but a copy. It should be fairly obvious at this point that a "Bad Thing"� can happen if either the original value or the copied value changes; there will be some unexpected data synchronization problems. In order to prevent this kind of problem, Java requires you to mark local variables that will be used by the AIC as final (i.e., unchangeable). This guarantees that the inner class' copies of local variables will always match the actual values.
Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
Thank you very much for the explanations!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why local inner classes CAN "see" final variables?
 
Similar Threads
A funny question,see it!!
About final variable
Method-Local Inner Class question
method local inner class
Why are finals allowed for method-local classes??