Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method-local Inner Class Q

 
Graeme Jenkinson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...
 
Graeme Jenkinson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic