views:

452

answers:

4

I have the following code. I want to get hold of the outer class object using which I created the inner class object inner. How can I do it?

public class OuterClass {

    public class InnerClass {
     private String name = "Peakit";
    }

    public static void main(String[] args) {
     OuterClass outer = new OuterClass();
     InnerClass inner = outer.new InnerClass();
       // How to get the same outer object which created the inner object back?
     OuterClass anotherOuter = ?? ;

        if(anotherOuter == outer) {
             System.out.println("Was able to reach out to the outer object via inner !!");
        } else {
             System.out.println("No luck :-( ");
        }
    }
}

EDIT: Well, some of you guys suggested of modifying the inner class by adding a method:

public OuterClass outer() {
   return OuterClass.this;
}

But what if I don't have control to modify the inner class, then (just to confirm) do we have some other way of getting the corresponding outer class object from the inner class object?

+8  A: 

Within the inner class itself, you can use OuterClass.this.

I don't think there's a way to get the instance from outside the code of the inner class though. Of course, you can always introduce your own property:

public OuterClass getOuter() {
    return OuterClass.this;
}

EDIT: By experimentation, it looks like the field holding the reference to the outer class has package level access - at least with the JDK I'm using.

EDIT: The name used (this$0) is actually valid in Java, although the JLS discourages its use:

The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.

Jon Skeet
Thanks Jon ! But what if I don't have control to modify the inner class (check my edit).
peakit
@peakit: Then as far as I know, you're out of luck unless you use reflection. It feels like it's a violation of encapsulation though really - if the inner class doesn't want to tell you what its outer instance is, you should respect that and try to design such that you don't need it.
Jon Skeet
Thanks for confirming Jon.
peakit
@Jon, actually `this$0` **IS** a valid Java identifier; see JLS 3.8. http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.8
Stephen C
@Stephen: Thanks, editing.
Jon Skeet
+3  A: 

OuterClass.this references the outer class.

bmargulies
But only in/within the source of the OuterClass. And I don't think that is what the OP wants.
Stephen C
+1  A: 

Honestly, it just feels unnatural to want to do this. If the inner class is dependent on the object that made it, is making the partition really the best way to do what you want?

unholysampler
+1  A: 

I think this may be abuse of the use of an inner class. It seems to me that what you want to do is violating the fundamental concepts of Object-Oriented programming. If you want to access the outer class from the inner class then include a reference to the outer class so you have access to it. When you have to do tricky things like this it usually is a sign that you should reconsider your design choices.

Brian T. Hannan