This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Method_Inner_class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method_Inner_class" Watch "Method_Inner_class" New topic
Author

Method_Inner_class

vibhas karn
Greenhorn

Joined: Aug 11, 2008
Posts: 26
Hi everybody. I want to know that why the inner class object cannot use the local variables of the method the inner class is in??
I know that local variables of the method live on the stack and exisist only for the lifetime of the method.
But in this following code the life time of the main method() exisist through out the inner class ends then it is not compiling.
Here is the code

[edit]Add code tags. CR[/edit]
[ September 23, 2008: Message edited by: Campbell Ritchie ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40

But in this following code the life time of the main method() exisist through out the inner class ends then it is not compiling.


Yes, but you are assuming that the compiler has special code to detect whether any instances of the inner class escapes the method. Maybe in future versions of Java, this will be added -- but currently no. So currently, accessing local variables (which are not final) is not allowed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vibhas karn
Greenhorn

Joined: Aug 11, 2008
Posts: 26
I am very sorry Henry i am not able to make out what exactly you are telling me can you please explain this a bit briefly.
Also can you expalin me why a inner class can't be public,private or static but it could be final and abstract.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40

I am very sorry Henry i am not able to make out what exactly you are telling me can you please explain this a bit briefly.


I can't elaborate, if I don't know what you don't understand. Otherwise, all I can do is just repeat what I said.

Henry
 
jQuery in Action, 2nd edition
 
subject: Method_Inner_class