• 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 local inner classes

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting lots of problem in 8th chapter of K&B book .
the latest one is :

method local inner class don't have cacess to local members ( variables ) of method .


the reason is given that object are always live in GCH even if you shutdown & restart your computer ( am i right ?) ( remains live untill garbage collection process happens ) & that local variables life is too short & they get lost when the method terminates . So ....

can any body complete this sentence ... please

& why the problem get solved when we make the variable final ... ?

please help me out ...

thank you very much in advance also & i will give later also ...
[ January 11, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi,

No, a Java object cannot live through your shutting down and rebooting your computer. Never. If the JVM has to restart, the code must be re-run.

As for the stack, it's quite simple, things that are local on the stack when a method is runnig become irrelevant once the closing bracket of the method is reached. This is a scope issue.

Put it this way, if a method has ececuted correctly, and is out of scope, you can't reference it's result, whatever you do.

The way I understand a method local inner class is sort of, "oh, I have to do something here, then I can throw it away because I'll never need it again".

If the local variable is final, it's like defining a constant (const). A constant never changes value.

Others?

Marcus
[ January 11, 2005: Message edited by: Marcus Laubli ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first of all , thank you marcus ... it was good .

Originally posted by Marcus Laubli:

The way I understand a method local inner class is sort of, "oh, I have to do something here, then I can throw it away because I'll never need it again".



what is that somthing special for that a new concept has introduced in java .


If the local variable is final, it's like defining a constant (const). A constant never changes value.


is it like that ...
Ok final variable are constant ( no body can change them ). They also live on stack ( i think ) & even after method is terminated , if any local inner class object are still using them , then also there is no problem because they are safe ( as per the expectation ).

please tell me if i am wrong or right in that ...

thank you very much once again .
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The principle is this: if you have an inner local class (declared in a method body) this class cannot have static members, because it cannot provide services for classes outside the scope of the method, however it can have static final field members (because they work as constants). Now, this inner local class can acccess the local variables and parameters declared in its containing context only if they are declared final. Objects of this class can access this values although the class is out of scope because the method is already finished.

Take a look at this snippet I wrote:

A just hope it looks allright because I wrote that code from my iPaq and here I cannot see its indentation.

Note that although the sample object (in new Sample()) its already elegible for garbage collection, it is still possible to access to the local variable values declared in the showMeLight() method.

( tags added and indentation tweaked )
[ January 11, 2005: Message edited by: Barry Gaunt ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic