Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, how do I access the outer class when I'm not in the inner class?

If I have an instance of an inner class, how can I access the outer class from code that is not in the inner class? I know that within the inner class, I can use Outer.this to get the outer class, but I can't find any external way of getting this.

For example:

public class Outer {   public static void foo(Inner inner) {     //Question: How could I write the following line without     //  having to create the getOuter() method?     System.out.println("The outer class is: " + inner.getOuter());   }   public class Inner {     public Outer getOuter() { return Outer.this; }   } } 
like image 455
Kip Avatar asked Apr 18 '09 14:04

Kip


People also ask

Can outer class access members of inner class in Java?

Conclusion. Outer classes can access inner class private members in Java until a non-static member accesses it from a static context or an inaccessible scope.

How do you access the variables of an outer class?

You can access the static variable of an outer class just using the class name.

How do you access the outer class variable in an inner static class?

Unlike inner class, a static nested class cannot access the member variables of the outer class. It is because the static nested class doesn't require you to create an instance of the outer class.

Can you override an inner class?

No, you cannot override private methods in Java, private methods are non-virtual in Java and access differently than non-private one. Since method overriding can only be done on derived class and private methods are not accessible in a subclass, you just can not override them.


2 Answers

The bytecode of the Outer$Inner class will contain a package-scoped field named this$0 of type Outer. That's how non-static inner classes are implemented in Java, because at bytecode level there is no concept of an inner class.

You should be able to read that field using reflection, if you really want to. I have never had any need to do it, so it would be best for you to change the design so that it's not needed.

Here is how your example code would look like when using reflection. Man, that's ugly. ;)

public class Outer {     public static void foo(Inner inner) {         try {             Field this$0 = inner.getClass().getDeclaredField("this$0");             Outer outer = (Outer) this$0.get(inner);             System.out.println("The outer class is: " + outer);          } catch (NoSuchFieldException e) {             throw new RuntimeException(e);         } catch (IllegalAccessException e) {             throw new RuntimeException(e);         }     }      public class Inner {     }      public void callFoo() {         // The constructor of Inner must be called in          // non-static context, inside Outer.         foo(new Inner());      }      public static void main(String[] args) {         new Outer().callFoo();     } } 
like image 65
Esko Luontola Avatar answered Sep 21 '22 17:09

Esko Luontola


There is no way, by design. If you need to access the outer class through an instance of the inner one, then your design is backwards: the point of inner classes is generally to be used only within the outer class, or through an interface.

like image 37
Michael Borgwardt Avatar answered Sep 23 '22 17:09

Michael Borgwardt