Himai Minh wrote:Example:
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...
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.)
}
}
salih ayan wrote:
// what "K&B book" wanted to express to us by telling (and then stored in an instance variable.)
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.
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
|