• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

method local inner class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why a method local inner class can access final variables of enclosing method?
 
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

It can access those variables because they are in scope. It does not access non-final local variables because there is the risk that the variable will be deleted from the stack before the lifetime of any objects of that class finishes.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes lives on the heap as they're object type while methods live on the stack which are two different contexts and places on the memory. The value of non-final method variables might change during the lines, while the final method variables will have the same fixed value which is reliable to use for inner class in another place in memory(heap).

Cheers
 
Aniket Kedari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for information.
But final variables of a method are also stored on stack. So isn't there a risk that the final variable will be deleted from the stack before the lifetime of any objects of that class finishes.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variables are actually captured into the inner class object, so the inner class object does not reference them on the stack. This means they can be disposed of on the stack safely. It also explains why they need to be final; if they were allowed to change, then the captured versions could become different to the ones on the stack.
 
Aniket Kedari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Peter
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aniket Kedari wrote:Thanks for information.
But final variables of a method are also stored on stack. So isn't there a risk that the final variable will be deleted from the stack before the lifetime of any objects of that class finishes.




Hi Guys,
Its an old post, Though i got to read this one and woukld like to answer this one clearly.
As method local inner class are given the final local variable copy to be stored into thier hidden private variables, corresponding parameters are added into the construtor call as well, as to get them initialized. So inner class now saperately stored final variable copy into its private members. If stach call of method dies and so the local variable also, it wont effect the private copy accessed through any still alive heap object of inner class.
Ranch's please correct if i am mistaken here..
Thanks.
 
I've never won anything before. Not even a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic