• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tanya Shetty
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The same, but without interface:

 
Henry Wong
author
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:[EDIT: Wow. Beaten to the answer twice. And with examples too... ]


You need Typing Tutor (just kidding )...
 
Tanya Shetty
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic