• 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

Method_Inner_class

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic