• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method_Inner_class

 
vibhas karn
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vibhas karn
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic