File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP- Method local Inner Classes - How can the inner class obj reference be saved. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP- Method local Inner Classes - How can the inner class obj reference be saved." Watch "SCJP- Method local Inner Classes - How can the inner class obj reference be saved." New topic
Author

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

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1017
    
  15
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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
Bartender

Joined: Sep 06, 2012
Posts: 1017
    
  15
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP- Method local Inner Classes - How can the inner class obj reference be saved.