• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2299
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example:
 
salih ayan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



 
author
Posts: 23936
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
salih ayan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23936
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 23936
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2299
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Greenhorn
Posts: 6
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Check out this code about passing reference of an object of a method local inner class.



Hope it helps.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic