• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of final in anonymous class

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wendy Gibbons wrote:
Thanks Jeff, never knew that



You're very welcome. Happy to help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic