• 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

Method local inner class accessing final local variable of methods.

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As we know method local inner class can
a) Access the instance variables of outer class[ This is usual for any type of inner class]
b) Cannot access local variables of method in which its defined [because local variables live in stack and die after method exceution]
Howeve method local inner class can access final local variables declared in the method, in which the class is defined.
Does this mean, that final local variables
a) Live in heap and not in stack, even when they are local variables?
b) If they live in heap why cant we access these variables from some other methods?, Why can we access them from method local inner class, As method local inner class object can still live even after method terminates.
Hope my question is clear.
Thanks
Deepak
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer to topic: https://coderanch.com/t/263835/java-programmer-SCJP/certification/Method-local-inner-class
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh,
Thats the reason only final local variables and local variable can be accessed from method local inner class, because they are final and cannot be modified so JVM will simply copy the value for the final variables.
And yes final local variables live in stack and not in heap.

After thinking for a while i understood why this behavior is done only for final and not local variables.

Thanks
Deepak
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic