• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Special treatment of final vaiables by method local inner classes??

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method local inner class can't use local variables of the method the inner class is in since local variables
live on the stack and exist only for lifetime of the method but Inner class object may live longer.(Page no:671 K&B)

then why local final variables can be accessible to inner class methods...
Can anyone explain this special behaviour??
 
Ranch Hand
Posts: 434
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hareendra Reddy wrote:A method local inner class can't use local variables of the method the inner class is in since local variables
live on the stack and exist only for lifetime of the method but Inner class object may live longer.(Page no:671 K&B)

then why local final variables can be accessible to inner class methods...
Can anyone explain this special behaviour??



The way I understand it, finals are like constants, they don't ever change, whereas anything on the stack will come and go (i.e. exist only for the lifetime of the method)
 
Saloon Keeper
Posts: 14270
321
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you declare the variables final, the inner class knows they won't change, so they can just make a copy of it for private use, which will exist after the original value has been removed from the stack.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@seema
Here i mean local variables which are declared as final,
but the scope of variable is within the method it self and they also
vanish like any other variables i suppose..

@Stephan
I am convinced with your logic ,what if those final variables are references to an object??

One more clarification, Objects are always created on the heap, then why is this restriction
applied to reference variables at all??

 
Stephan van Hulst
Saloon Keeper
Posts: 14270
321
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because we never work with objects directly in Java. We always have references to objects. While objects always exist on the heap, local references to them are on the stack like any other variable, and will be removed once the method is finished. When you declare the variable final, the inner class knows it can make a copy of the reference, not the object.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan...
Method local inner classes have additional advantage that they can access
fields of the outer classes, they can even access local variables....
For that to happen Inner classes must make a copy of the instance fields...
As local variable that is declared final cannot be modified after it has been
initialized,It is guaranteed that local variable and copy that is made inside
local class has the same value....

No i am fully convinced Stephan
 
Stephan van Hulst
Saloon Keeper
Posts: 14270
321
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got it, except for one thing.

Inner classes don't have to make copies of instance fields, because they already have a reference to their containing class, and the containing class will never stop existing before the inner class does.

They only make copies of local variables, because those variables will cease to exist when the method containing them returns.
 
Slime does not pay. Always keep your tiny ad dry.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic