• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

about method-local inner class can not use variables declared within the method

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if my understanding is right, it would be really appreciated if you could correct me:
Here is my understanding:
1) when method ends, the stack for the method is gone, the memory address storing the value of the method's local variable will be used for something else whereas the method-local object is still alive and reference to that memory address, which causes the corruption.
2) If the method's local variable is marked as final, the method-local object will have a different variable stored in the heap reference to the same memory address in the stack and this memory address in the stack will not be recycled when method ends .
Here is the question:
From what I can see is that ONLY primitive local variables will be re-cycled after the method ends, the reference variables declared in the method will still be usable since the object they are referencing to is in the heap and after the method-local object is created, they have there own reference variable refer to the object in heap and it will not be recycled. It seems like restricting user from using primitive local variables only will suffice, is it?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no difference between primitive local or reference local variables. When the method finishes, all local variables are destroyed (but a copy of final local variables is provided for method local inner classes.) If the method has reference local variables, and the objects which they point to are not pointed to by any other reference variable, then the objects themselves will become eligible for GC.
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:.... all local variables are destroyed (but a copy of final local variables is provided for method local inner classes.) ....


Can you please explain more about how final variables are stored in the stack and why it will not be destroyed after method ends?

Ruben Soto wrote: ..... If the method has reference local variables, and the objects which they point to are not pointed to by any other reference variable, then the objects themselves will become eligible for GC....


I thought the inner class object will have its own reference variable point to the same object as the local reference variable pointed to, since the inner class object is alive, the object will not be recycled by GC.

Thank you Ruben.
 
reply
    Bookmark Topic Watch Topic
  • New Topic