This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem in method local inner classes ? 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 "problem in method local inner classes ?" Watch "problem in method local inner classes ?" New topic
Author

problem in method local inner classes ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

I'm studying inner classes from kb6 book and i have a doubt regarding inner classes. the book says that method local inner classes cannot access local variables of the method because local variables live on stack and they wont be as long available as the object of the inner class itself and hence the reason. however it further says that if we mark the local variables final , then the object of the method local inner class can access the local variable. i would like to know why ? after putting final still the local variable is local variable and its scope and lifetime still will be same as it was before putting final and still would live less than method local inner class object. Please explain ?


following are few lines from the book itself.

Because the local variables aren't guaranteed to be alive as long
as the method-local inner class object, the inner class object can't use them. Unless
the local variables are marked final! The following code attempts to access a local
variable from within a method-local inner class.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40


Please search first, as this questions comes up often.... BTW, here the first one that I found.

http://www.coderanch.com/t/572124/java/java/local-variable-scope

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Consider Paul's rocket mass heater.
 
subject: problem in method local inner classes ?