File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes regd  local inner class (method level) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regd  local inner class (method level)" Watch "regd  local inner class (method level)" New topic

regd local inner class (method level)

Ramakrishnan Ponmudi
Ranch Hand

Joined: Apr 20, 2004
Posts: 72
why final local variables are only accessed by local inner class
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
The instance of the anonymous class may live longer than the method, and therefore longer than the local variable. To circumvent this live time problem, the instance actually gets a *copy* of the local variable. If you were allowed to change the local variable, that could lead to confusing inconsitencies between the original and the copy inside the instance of the anonymous class. Therefore you are not allowed to change the value of the local variable - it needs to be final.

Does that help?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Unfortunately your display name is not valid.

We require display names to be two words: Your first name, a space, then your last name. Fictitious names are not allowed.

I have already warned you here. Please change it immediately since accounts with invalid display names get deleted, sometimes without warning.

Consider Paul's rocket mass heater.
subject: regd local inner class (method level)
jQuery in Action, 3rd edition