aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "local inner class" Watch "local inner class" New topic
Author

local inner class

Anne Sullivan
Greenhorn

Joined: Mar 29, 2007
Posts: 9
Does a local inner class which resides inside a static method still have the "can only access local variables that is declared final" restriction?
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
A method local inner class can access the local variables that are marked final in the method that the class is defined.

However the method local inner class cannot access the local variables of
the class because the local variables are declared on stack and dies once the method completes execution, If JVM would have allowed access to local
variables then there is a possiblity that the object declared of the class
may be passed outside the method and this case the local variable would have be destroyed and that object would laterd try to modify the variable.
Only final varaibles are allowed because they are final, Compiler would replace the final variables with constants and since final variables cannot be modified its perfectly leagal for JVM to allow the access of final local variables by method local inner class.
Thanks
Deepak


SCJP, SCWCD, SCBCD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: local inner class
 
Similar Threads
Inner Classes
Inner class
Method Local inner class
method-local inner class object cannot use the local variables
regd local inner class (method level)