Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic