• 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

SCJP- Method local Inner Classes - How can the inner class obj reference be saved.

 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question regarding method local inner classes. In page 671 of SCJP book, K&B states that non final method local variables are not visible inside a method local inner class and that the inner class object might still be alive on heap somewhere even after jvm has winded the stack associated with the method. This is why a method local inner class can access only the final of the local variables.

So then what is the scope of the method local final variables? Do they live longer than other method local variables? And how can the reference of a method local inner class object be assigned to an instance field? I tried declaring InnerClass obj in OuterClass but it wouldn't recognize it.



So then how can I assign obj to another instance reference? What would be the scope of obj if it is not assigned to an instance reference? Would it be eligible for garbage collection once the control moves out of the method?

Thanks in advance.
Chan.
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:And how can the reference of a method local inner class object be assigned to an instance field? I tried declaring InnerClass obj in OuterClass but it wouldn't recognize it.



So then how can I assign obj to another instance reference? What would be the scope of obj if it is not assigned to an instance reference? Would it be eligible for garbage collection once the control moves out of the method?




If you assign the object to a reference that is not at method scope, then obviously, it can't be garbage collected after the method call completes. It can only be garbage collected when it is no longer reachable.

And BTW, the definition of the inner class will go out of scope when the method completes. So, you can't assign it to an InnerClass reference. You must assign it to a Object reference, or something that IS-A that instance that is still in scope.

Henry
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:
I have a question regarding method local inner classes. In page 671 of SCJP book, K&B states that non final method local variables are not visible inside a method local inner class and that the inner class object might still be alive on heap somewhere even after jvm has winded the stack associated with the method. This is why a method local inner class can access only the final of the local variables.

So then what is the scope of the method local final variables? Do they live longer than other method local variables?



The scope of method local variables are for the method call only. They don't live longer. What is happening is, the instance of the inner class (for final variables that has already been initialized) will make a copy of the variable's value.

Henry
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry for such a quick response. I understand the idea now.

To test the assignment to an Object reference, I coded my class as follows.



So java allows me to save this object. Thinking of the practicality, the only thing that comes to my mind is that I can use such saved objects with a cast in the same method in later invocations. But may be it'll become more clear when I'm actually working on this stuff.

Thanks..
Chan.
 
reply
    Bookmark Topic Watch Topic
  • New Topic