• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of final in anonymous class  RSS feed

 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


We have to mark f and f1 as final to access them in the anonymous class. Why is this?

Thanks
Chandra
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are local variables. That means they live on the stack and cease to exist as soon as the method ends. However, the anonymous inner class instance can (and usually does) continue to exist long after the method ends. So the inner cannot use the same variables as the method, since they will cease to be valid while the inner still needs them.

The approach that Java takes to resolve this is to make a separate copy of those variables for the inner. However, now we have the problem that we have two independent copies of what's supposed to be the same variable. If one copy changes, the other copy won't know about it, which is not desired behavior for what our code views as the same variable. So to solve that, Java requires that the variables be final. That way, the inner can get its own copy, and it will always have the same value that the method sees, since neither one can change.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra shekar M wrote:We have to mark f and f1 as final to access them in the anonymous class. Why is this?

And just to add to Jeff's excellent explanation; the fact is - you don't:will work just as well...albeit at cost to your carpel tunnels.

Winston
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Those are local variables. That means they live on the stack and cease to exist as soon as the method ends. However, the anonymous inner class instance can (and usually does) continue to exist long after the method ends. So the inner cannot use the same variables as the method, since they will cease to be valid while the inner still needs them.

The approach that Java takes to resolve this is to make a separate copy of those variables for the inner. However, now we have the problem that we have two independent copies of what's supposed to be the same variable. If one copy changes, the other copy won't know about it, which is not desired behavior for what our code views as the same variable. So to solve that, Java requires that the variables be final. That way, the inner can get its own copy, and it will always have the same value that the method sees, since neither one can change.


Thanks Jeff, never knew that
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Gibbons wrote:
Thanks Jeff, never knew that


You're very welcome. Happy to help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!