Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Method local inner class and accessing local variables

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I understand that method local inner classes (i.e. inner classes defined from within a method) can't access the method's local variables, since the local variables live on the stack and are bust after the method is done, but the instance of the method local inner class could live much longer (since a reference could be returned and used).

But I also read that while the method local inner classes can't access a local variable, there is an exception to the rule - that the method local inner class could access a "final" local variable. How come? Don't all local variables including "final" local variables live on the stack? So how can a method inner classe's instance access a "final" local variable, even though it can't access non-final local variables.

Could someone please throw some light on this?

Thanks
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method local inner class gets a copy of the final variables of the enclosing method. Since it is known that the value of the final variable won't change, so when an instance of the method local inner class is created, it gets a copy of the final variables of the class which the method local inner class stores as instance fields. This is all hidden from us and is done automatically by the compiler...
 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. One of those mysteries of java that happens under the covers. Thanks.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:The method local inner class gets a copy of the final variables of the enclosing method. Since it is known that the value of the final variable won't change, so when an instance of the method local inner class is created, it gets a copy of the final variables of the class which the method local inner class stores as instance fields. This is all hidden from us and is done automatically by the compiler...



So, it is an attribute of that method local inner class?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:So, it is an attribute of that method local inner class?


Yes they (i.e. final local variables of a method) becomes an instance field of the method local inner class...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit......
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic