wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Local Inner Class Pg 671 Kathy Sierra and Bates SCJP 6 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 Pg 671 Kathy Sierra and Bates SCJP 6" Watch "Method Local Inner Class Pg 671 Kathy Sierra and Bates SCJP 6" New topic
Author

Method Local Inner Class Pg 671 Kathy Sierra and Bates SCJP 6

Tanya Shetty
Ranch Hand

Joined: Jun 17, 2009
Posts: 40
What Method local inner classes CAN and CANNOT DO section -->
Method local inner classes cannot access local variables of that method. .... ....., even after the method completes, the inner class object created within it might still be alive on the heap if a reference to it was passed into some other code which stores it in an instance variable...


I have tried declaring an instance variable of the type of method local inner class, but outside the method. It gives a compile error


Clearly, you cannot access even the type of the method local inner class outside the method in which it is declared. In the quotes it is mentioned, what if the object which is created inside the method , gets its reference passed outside the method, how is that possible when we cannot access the type of this method outside the method itself.

This means, once the method is complete, the local reference variable, to which the method local inner class object was assigned inside the method, dies when the method is complete. Thus, the object is abandoned.And if this is the case always, why , method local inner class objects cannot use the local variables which also die after the method is complete???

Am i not seeing another scenario in which the method local object can be passed out of the method ??? Please explain!! Thanks a ton!
vic Gar
Greenhorn

Joined: Feb 26, 2005
Posts: 1
I'm not sure if I understood you, but in case I did, here's an example that I think demonstrates what you're asking for:



SCJP1.6; working on SCWCD
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423



The same, but without interface:

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Tanya Shetty wrote:Clearly, you cannot access even the type of the method local inner class outside the method in which it is declared. In the quotes it is mentioned, what if the object which is created inside the method , gets its reference passed outside the method, how is that possible when we cannot access the type of this method outside the method itself.


Just because the type of the method local inner class is out of scope, doesn't mean that it can be accessed -- the method local inner class type still inherits from the Object class, and hence, can be held in an Object instance variable.

Tanya Shetty wrote:
This means, once the method is complete, the local reference variable, to which the method local inner class object was assigned inside the method, dies when the method is complete. Thus, the object is abandoned.And if this is the case always, why , method local inner class objects cannot use the local variables which also die after the method is complete???


What do you mean "the object is abandoned"?

Now, of course, this example isn't that clear... for example, let's say the method inner class also implements Runnable, then it can also be held in a Runnable instance variable too. And what if this run() method needs to access the local variable?

[EDIT: Wow. Beaten to the answer twice. And with examples too... ]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Henry Wong wrote:[EDIT: Wow. Beaten to the answer twice. And with examples too... ]


You need Typing Tutor (just kidding )...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tanya Shetty
Ranch Hand

Joined: Jun 17, 2009
Posts: 40
Hey Thanks!! I really appreciate the spontaneous help!! And now I figured it out.. The idea of Object class and casting completely slipped from my mind

 
GeeCON Prague 2014
 
subject: Method Local Inner Class Pg 671 Kathy Sierra and Bates SCJP 6