• 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

Class within a method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can a class within a method only see the final variables of the enclosing method.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See my discussion about half way down the thread about final variables in loops.
Bill
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Douglas Dunn talks about this in an article I read. It is actually done so as to avoid programmer confusion. Local variables are not actually accessed directly by the inner class. They are passed in to the inner class in the same way that variables are passed in as parameters to a method... by value. So in order to avoid confusion, the JLS requires local variables used by an inner class to be marked as final.
[ April 09, 2003: Message edited by: Thomas Paul ]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This what I am talking about:

If this would compile (it doesn't because x isn't final) it would print:
0
0
And that is why x must be final.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables of the method live on the stack, and exist only for the lifetime of the method. Thus scope of the local variable is limited to lifetime of the method. But the Inner class objects,created on the heap, might still be alive even after the method completes. (for ex a reference to it was passed to other code).
This complication prevents method local variables from being used by the Method Local Inner Class unless the variable are marked final
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Neeraj Kumar Srivastava:
Local variables of the method live on the stack, and exist only for the lifetime of the method. Thus scope of the local variable is limited to lifetime of the method. But the Inner class objects,created on the heap, might still be alive even after the method completes. (for ex a reference to it was passed to other code).
This complication prevents method local variables from being used by the Method Local Inner Class unless the variable are marked final


This is incorrect. An analysis of the generated code will show that this is not true. Variables are passed to inner classes the same way that parameters are passed to any method.
Let's look at the common wisdom from above. The same potential problem would exist if you were passing parameters to any method and they were used in a thread for example that outlived the calling method! Parameters are always passed by value in Java so when they are passed copies are made. The same goes for inner classes. Copies are made and given to the inner class so there is no problem with stack variables expiring.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Variables are passed to inner classes the same way that parameters are passed to any method.


Surely you don't mean that. Parameters are passed to methods on the stack. Inner class instances get constructed with a reference to the containing object and copies of local variables as they exist at the moment of construction.
Oh - it just occurred to me that maybe what you want to say is they get a copy, just like method calls get a copy - instead of a reference to the original variable.
Bill
[ April 10, 2003: Message edited by: William Brogden ]
 
No holds barred. And no bars holed. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic