Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regd local inner class (method level)

 
Ramakrishnan Ponmudi
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why final local variables are only accessed by local inner class
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ramakrishnan",
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.

thanks,
Dave.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic