This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Java in General and the fly likes About inner classes.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "About inner classes...." Watch "About inner classes...." New topic

About inner classes....

roy neela

Joined: Mar 26, 2008
Posts: 3
Inner class declared within a method cannot access the method's local variable....But if the method's local variable is declared 'final',then it can be accessed by the inner class...... Any reason for this behaviour?!
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107

Because accessing local variable in the "local class" (inner class declaring in the method) is converted by passing parameters to the local class constructor (and saved in the hidden field).

If local variable were not final, something like "b=b+1" couldn't affect local class field, and the code wouldn't run what we intended.
It is sorta covered in the JavaRanch Style Guide.
subject: About inner classes....