It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method-local Inner Class Q 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 "Method-local Inner Class Q" Watch "Method-local Inner Class Q" New topic
Author

Method-local Inner Class Q

Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
Sierra & Bates Cert Guide 6 Chapter 8 Inner Classes p671 states:

"But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable."

How would you reference the method-local inner class outside of the method? For example, how would I declare an instance variable in the class MethodLocalTest to store the instance of MethodLocal class instantiated as below (this is off the top of my head so please ignore any silly mistakes)?



I can't find anyway of doing this (for example, MethodLocalTest.MethodLocal) and a quick look on the web suggests that the statement in S&B may not be correct. Does anyone know for sure (else I'll have a look in the langauge spec).

thanks,

Graeme
[ October 20, 2008: Message edited by: Graeme Jenkinson ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17



In the above code an instance of the method local class still remains alive even if the method call returns. Even a better example would be



The above code will display Method local go... This proves that an instance of the method local class was alive even after the method call returned...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
Yeah suposed I get the point (I realised after posting that by upcasting you could return a reference to the instance and therefore it would continue to live on the heap) but wouldn't this be kind of pointless as you couldn't do much useful with it?
[ October 20, 2008: Message edited by: Graeme Jenkinson ]
Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
of course method local inner class is not much useful. But i think the following example would give you a better understanding


Now, you can invoke the methods of the interface on the method local inner class object. Hope i'm clear.


-Aravind<br />SCJP 5.0 (91%), Preparing for SCBCD.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method-local Inner Class Q