File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Inner Classes &Final  Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Inner Classes &Final  Variable" Watch "Method Inner Classes &Final  Variable" New topic
Author

Method Inner Classes &Final Variable

Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Hi to all.

The method local inner classes can only access the final variables of the method enclosing.
Why is it so?

what is the special treatment to final variables?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Final variables are essentially constants. Constants are handled a little differently than variables (certainly in how they are stored) and this may be the explanation for why local classes are allowed to access final variables but not others.

For one thing, local variables are found on the activation stack while constants are put into a constant table. My guess is that, when a local class method is invoked, any variables that would normally be found in the local activation record would be inaccessible while the constant values in the constant table are reachable.

I might be quite wrong in this - I didn't build Java. But, then again, that's my best guess for the time being.


SCJP Tipline, etc.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Mmmm. Not entirely. Final local variables live on the stack like any other local variable.

You see, the access that inner classes have to local (method) variables and parameters is an illusion. You don't have any access to local variables. What happens behind the scenes is that the Java compiler creates instance variables in your inner class which contain copies of the local variables you "access".

The problem is, if either the method or the inner class were allowed to change its copy of the variable, the different copies would get out of sync and the illusion would be shattered. This is why the compiler forces you to declare the variable final: so that it can freely generate multiple copies and create the illusion that your inner class has access to them.

The inner class implementation is full of such trickery. It had to be, because Sun added them to the Java language without changing the JVM spec. For example, the special access that inner (and nested) classes have to private variables of their enclosing class is... you guessed it... an illusion. Access is granted through special accessor methods generated by the compiler.

If you don't believe me, use javap to disassemble some class files that have inner classes

- Peter
Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Thaks to all of you for this lively discussion.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
but in k&B its written that we can't use method local variables becasue as soon as we return the object from any method its local variable are not return as..they have limited scope and can't be accessed from outside method...
i think this is right reason...for making compiler not accessing to method local variables ...


Thanks and Regards, Amit Taneja
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Inner Classes &Final Variable
 
Similar Threads
query about method local inner classes
Final variables
wrong answer???
final variables in method
Wrong Answer?