• 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

inner classes and local variables...

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method local inner classes cannot access the method's local variables. This is understandable as the local variables will be blown out of stack once the control comes out of the method.
But, I also found that local variables marked as final can be accessed by the method local inner class.
What is the reason for this? Won't the final variable, local to a method, be thrown out of the stack once control comes out of the method?
Pls explain.

Thanks in advance
Sriram.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the local variable (or method parameter) is marked final, then it is copied into the object instantiated from the method local class. You can pass primitives and object references in this way.
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry :-)
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
If the local variable (or method parameter) is marked final, then it is copied into the object instantiated from the method local class. You can pass primitives and object references in this way.



hmm btw how do we mark the method parameter as final?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Keith:


hmm btw how do we mark the method parameter as final?


like this:

private void test(final int a){}

in the body of method test, you cannot change variable a by assigning a new value to it.
reply
    Bookmark Topic Watch Topic
  • New Topic