GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes method-local inner class object cannot use the local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method-local inner class object cannot use the local variables" Watch "method-local inner class object cannot use the local variables" New topic
Author

method-local inner class object cannot use the local variables

bob jacosin
Greenhorn

Joined: May 05, 2009
Posts: 4
method-local inner class object cannot use the local variables
of the method the inner class is in. Why not?
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
The initiated objects of the class can live past the life of method invocation, once the method returns and the stack unwinds the local variables are gone, but the object is still alive. Local classes can however use final local variables.
bob jacosin
Greenhorn

Joined: May 05, 2009
Posts: 4
is it possible to use the method-local inner class as return type of the method where the inner class is in?
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
bob jacosin wrote:is it possible to use the method-local inner class as return type of the method where the inner class is in?


No but you can return the super type, or pass it as a parameter to a method that accepts its supper type or assign it to a instance variable ( of a type that is super class of your local class). If there is any outside reference to the local class object after the method finishes it will still be accessible.
bob jacosin
Greenhorn

Joined: May 05, 2009
Posts: 4
ahh ok...thats it i see thank you...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
bob jacosin wrote:ahh ok...thats it i see thank you...
If by "I see" you mean "I don't understand", please say so and ask for more explanation.
 
GeeCON Prague 2014
 
subject: method-local inner class object cannot use the local variables