wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can we pass a metod local inner class instance to some other code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can we pass a metod local inner class instance to some other code" Watch "How can we pass a metod local inner class instance to some other code" New topic
Author

How can we pass a metod local inner class instance to some other code

salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
In "SCJP SCJD 1.6 Kathy Sierra & Bert Bates" book. In that book page 671 :

What a Method-Local Inner Object Can and Can't Do

A method-local inner class can be instantiated only within the method where the inner
class is defined. In other words, no other code running in any other method—inside
or outside the outer class—can ever instantiate the method-local inner class. Like
regular inner class objects, the method-local inner class object shares a special
relationship with the enclosing (outer) class object, and can access its private(or
any other) members. However,the inner class object cannot use the local variables
of the method the inner class is in. Why not?

Think about it. The local variables of the method live on the stack, and exist only for
the lifetime of the method. You already know that the scope of a local variable is
limited to the method the variable is declared in. When the method ends, the stack
frame is blown away and the variable is history. 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.
Because the local variables aren't guaranteed to be alive as long
as the method-local inner class object, the inner class object can't use them. Unless
the local variables are marked final!The following code attempts to access a local
variable from within a method-local inner class.


How a reference to it was passed into some other code and then stored in an
instance variable.
How can we pass a metod local inner class instance to some other code

May you please explain me with a short example..
If you explain me l will be grateful to you.
Really l dont understand
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 592
Example:
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
Himai Minh wrote:Example:



Thanks alot Himai Minh.You really open horizon for me. By giving this example you clean in my mind about
(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 )


But still point about (and then stored in an instance variable.) not clean.

Okey you pass reference of metod-local-inner-class instance to some other code.But can we stored this an instance variable.

I tried below code seem impossible to compile .Any way exist to compile?



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

salih ayan wrote:
Thanks alot Himai Minh.You really open horizon for me. By giving this example you clean in my mind about
(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 )


But still point about (and then stored in an instance variable.) not clean.

Okey you pass reference of metod-local-inner-class instance to some other code.But can we stored this an instance variable.

I tried below code seem impossible to compile .Any way exist to compile?





You can't do that -- the class file definition is out of scope. All you can do is store the instance. You can't store the instance using the inner class definition.

Like so...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
You can't do that -- the class file definition is out of scope. All you can do is store the instance. You can't store the instance using the inner class definition.

Like so...




Ok you say that (You can't do that ) But why book saying (and then stored in an instance variable.). Ok I get as you say (You can't do that ) we agree that we cant do this than
what K&B book wanted to explain to readers.

Also, I am thankful to Himai Minh for his example but if metod-local-inner-class definition is invisible out of the method which metod-local-inner-class definition is in than
for what aim we pass reference of metod-local-inner-class instance to some other method.

Look below..

class Example{

public void aMethod(){

class AnInner{
//methods defined here...
}

AnInner innerInstance = new AnInner();
bMethod(innerInstance);

}
private void bMethod(Object obj){

// AnInner class invisible out of "aMethod()" than for what purpose we pass ( bMethod(innerInstance) ) it here.
// Finally we cant use the "AnInner" instance .
// Here what can be "K&B book" purpose by telling this.
// what "K&B book" wanted to express to us by telling (and then stored in an instance variable.)

}
}



Thanks for your replies
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39



First, your big red fonts are kinda annoying -- it feels like you are yelling at people. Can you please tone it down a bit?

salih ayan wrote:
// what "K&B book" wanted to express to us by telling (and then stored in an instance variable.)



And can you tell us why you think the example doesn't do that? I provided an example that stores the Inner object into an instance variable. Just because the reference is an Object reference, doesn't mean that it isn't an Inner object. You just can't use it as an Inner object when the class definition is out of scope.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

salih ayan wrote:
Also, I am thankful to Himai Minh for his example but if metod-local-inner-class definition is invisible out of the method which metod-local-inner-class definition is in than
for what aim we pass reference of metod-local-inner-class instance to some other method.


Keep in mind that classes are part of a inheritance hierarchy. Interfaces are also class types. So, when you create an instance, it may use lots of class definitions. Just because one class definition goes out of scope, doesn't mean that it can't be used. You can still use it with any class definition that is still in scope. This is why many inner classes supports (aka implements) interfaces. The method local inner class instance can still be used outside of the method via its supported interfaces.

Henry
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
Keep in mind that classes are part of a inheritance hierarchy. Interfaces are also class types. So, when you create an instance, it may use lots of class definitions. Just because one class definition goes out of scope, doesn't mean that it can't be used. You can still use it with any class definition that is still in scope. This is why many inner classes supports (aka implements) interfaces. The method local inner class instance can still be used outside of the method via its supported interfaces.


Perfect explanation really really thanks so much.Also thanks to Himai Minh

By the your and Himai's light l developed below code .Is it right?






Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 592
Yes. Your Outer class works.

(I am fine your red words. But if someone else don't like red, you can use bold or green/blue to make the words stand out.)
kaku kohli
Greenhorn

Joined: Oct 02, 2012
Posts: 6

Hello,
Check out this code about passing reference of an object of a method local inner class.



Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we pass a metod local inner class instance to some other code
 
Similar Threads
Method-Local Inner Class and Local Variable
why 'method local inner class' can only access final variable?
Method local inner classes
What a Method-Local Inner Object Can and Can't Do
Why are finals allowed for method-local classes??