Home arrow static arrow Java Programming [Archive] - referencing outer class from an inner class
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - referencing outer class from an inner class
This topic has 18 replies on 2 pages.    1 | 2 | Next »

Posts:284
Registered: 5/24/04
referencing outer class from an inner class  
Jul 21, 2004 9:01 AM



 
Hi,
How can I explicitly reference the "outer" class instance from an inner class?
the implicit reference won't do because I am calling a method that exists in both classes....

for example:
public Class Outer {   class Inner {      public void callOuterPrint() {         print(); // good invocation using the implicit refernce      }      public void callOuterGetClass() {         getClass(); // not good, this will invoke the Inner's getClass method      }   }    public void print() {      System.out.println("Hello");   }}
 

Posts:123
Registered: 5/24/04
Re: referencing outer class from an inner class  
Jul 21, 2004 9:16 AM (reply 1 of 18)



 
Do you remember that your teacher told you that non-static methods belonged to Objects and not to Classes?

Either you pass an object as parameter to the Inner's constructor, and save it there, or you only use final objects, or you make the methods it needs to call static.
 

Posts:24,036
Registered: 2/3/03
Re: referencing outer class from an inner class  
Jul 21, 2004 9:19 AM (reply 2 of 18)



 
I think this is what you're looking for...
class Outer {    public static void main(String[] args) {        Inner inner = new Outer().new Inner();        inner.printInnerClass();        inner.printOuterClass();    }     class Inner {        void printInnerClass() {            System.out.println(getClass());        }         void printOuterClass() {            System.out.println(Outer.this.getClass());        }     }}
 

Posts:192
Registered: 30/05/01
Re: referencing outer class from an inner class  
Jul 21, 2004 9:22 AM (reply 3 of 18)



 
how about
public class Outer {Outer outer;public Outer(){	outer = this;} class Inner {public void callOuterPrint() {print(); }public void callOuterGetClass() {outer.getClass(); }} public void print() {System.out.println("Hello");}}
 

Posts:24,036
Registered: 2/3/03
Re: referencing outer class from an inner class  
Jul 21, 2004 9:22 AM (reply 4 of 18)



 
P.S. I only use that as an example as to HOW to get a reference to the enclosing class; I'm not sure WHY you would want to actually call getClass()...
 

Posts:81
Registered: 6/9/04
Re: referencing outer class from an inner class  
Jul 21, 2004 9:22 AM (reply 5 of 18)



 
The two ways I know of are work arounds.
The first is to pass the reference in the inner constructor and save it.
The second is to put a method in the outer that returns this.

Assuming inner and outer have foo. This is all i can think of
class Outer{    class Inner{        public Inner(){            Outer o = getOuter();            o.foo(this);        }       Inner foo(Inner i){return i;}    }    Inner foo(Inner i){return i;}    private Outer getOuter(){return this;}}
 

Posts:123
Registered: 5/24/04
Re: referencing outer class from an inner class  
Jul 21, 2004 9:23 AM (reply 6 of 18)



 
Doh, misread. I hate being at work for more than 15h...

Shoot me.
 

Posts:284
Registered: 5/24/04
Re: referencing outer class from an inner class  
Jul 21, 2004 9:23 AM (reply 7 of 18)



 
Do you remember that your teacher told you that non-static methods belonged to Objects and not to Classes?
why do you think that I'm not aware of this?

Either you pass an object as parameter to the Inner's constructor, and save it there, or you only use final objects, or you make the methods it needs to call static.
I know I can use all these solutions you've mentioned but they will enforce changing design solely on a stupid technical issue. are you saying that what I want to do is not possible? simply because the method name is the same in both classes?
 

Posts:123
Registered: 5/24/04
Re: referencing outer class from an inner class  
Jul 21, 2004 9:26 AM (reply 8 of 18)



 
Read my other post ;p
 

Posts:81
Registered: 6/9/04
Re: referencing outer class from an inner class  
Jul 21, 2004 9:27 AM (reply 9 of 18)



 
The adding the return of "this" to a private funciton in the outer class seems reasonable.

1) it doesnt break existing code
2) if the routine gets deleted it will be an easy to spot and interpret in compiling.
3) it doesnt involve saving extra data.
 

Posts:284
Registered: 5/24/04
Re: referencing outer class from an inner class  
Jul 21, 2004 9:29 AM (reply 10 of 18)



 
Read my other post ;p
Yah, I saw It only after I posted.... :-)

thanks yawmark, Outer.this was what I was looking for....

thanks for all the replies
 

Posts:24,036
Registered: 2/3/03
Re: referencing outer class from an inner class  
Jul 21, 2004 9:31 AM (reply 11 of 18)



 
Just for emphasis, I'll repeat:

To get a reference to the enclosing class, just use "Outer.this".
 

Posts:24,036
Registered: 2/3/03
Re: referencing outer class from an inner class  
Jul 21, 2004 9:32 AM (reply 12 of 18)



 
The adding the return of "this" to a private funciton
in the outer class seems reasonable.

It's just unnecessary.
 

Posts:24,036
Registered: 2/3/03
Re: referencing outer class from an inner class  
Jul 21, 2004 9:39 AM (reply 13 of 18)



 
Another example:
class Outer {     public static void main(String[] args) {        Inner inner = new Outer().new Inner();        inner.snuffTheNaughty();    }     void lobHolyHandGrenade() {        System.out.println("Boom!");    }     class Inner {        void snuffTheNaughty() {            Outer.this.lobHolyHandGrenade();        }    }}
 

Posts:24,036
Registered: 2/3/03
Re: referencing outer class from an inner class  
Jul 21, 2004 9:39 AM (reply 14 of 18)



 
How's that for unnecessary? LOL :o)
 
This topic has 18 replies on 2 pages.    1 | 2 | Next »