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 access of non final variable from method local inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "access of non final variable from method local inner class" Watch "access of non final variable from method local inner class" New topic
Author

access of non final variable from method local inner class

rohit shekhar
Ranch Hand

Joined: Mar 05, 2010
Posts: 32


above code is generating error unless x is not final. In book what they explained I am not able to understand. Please explain.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
If its not too much of an effort, do type out the gist of what was given in the book and what you didn't understand. It'll help the folks here to get to heart of the issue, right away.

Thanks,
Praveen.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Local inner classes cannot access the local variables defined in the method in which the local classes are defined. the reason is when the local inner class in instantiated , the object as you know is created on the heap. the thing is that this object can have more lifetime than the local variables which defined in the method , which are created on the stack and which once method completes are blown off the stack . so you cannot access local variables from local inner classes. however if you make the variables final, then the local inner classes can access them. this is because , in this case, the object of the inner class gets reference to the local variable that has been declared as final . so even after the method completes the object can access the local variable. the point is that the actual variables has been lost but the reference is there with the inner class object and it seems as if we are accessing the original variable. you can disassemble the class file using javap and you can see the result.

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

http://javarevisited.blogspot.in/2011/12/final-variable-method-class-java.html


OCA7
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

ankita modi. wrote:http://javarevisited.blogspot.in/2011/12/final-variable-method-class-java.html


thats been few months back ankita. the person probably does not need the answer.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

gurpeet singh wrote:
ankita modi. wrote:http://javarevisited.blogspot.in/2011/12/final-variable-method-class-java.html


thats been few months back ankita. the person probably does not need the answer.


Okey dokey nice
 
jQuery in Action, 2nd edition
 
subject: access of non final variable from method local inner class