Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting hold of the outer class object from the inner class object

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?

like image 353
peakit Avatar asked Nov 29 '09 19:11

peakit


People also ask

How do you access the outer class from the inner class?

Since inner classes are members of the outer class, you can apply any access modifiers like private , protected to your inner class which is not possible in normal classes. Since the nested class is a member of its enclosing outer class, you can use the dot ( . ) notation to access the nested class and its members.

Can we create object of a inner class outside of the outer class?

You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class. Following is the program to create an inner class and access it.

How do you create an inner class instance from outside the outer class instance code?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

Can inner class access outer class variables?

In inner classes, variables of outer class are accessible, but local variables of a method are not.


1 Answers

Within the inner class itself, you can use OuterClass.this. This expression, which allows to refer to any lexically enclosing instance, is described in the JLS as Qualified 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 pre-existing names on legacy systems.

like image 144
Jon Skeet Avatar answered Oct 14 '22 06:10

Jon Skeet