Java Reflection - Java Class.getEnclosingClass()








Syntax

Class.getEnclosingClass() has the following syntax.

public Class <?> getEnclosingClass()

Example

In the following code shows how to use Class.getEnclosingClass() method.

public class Main {
  public static void main(String[] args) {
    Main cls = new Main();
  }//w  w w.  j  av  a 2 s. com
}

class MyClass {
  public MyClass() {
    class Outer {
      public void show() {
        class Inner {
          public void show() {
            System.out.print(getClass().getName() + " inner in...");
            System.out.println(getClass().getEnclosingClass());
          }
        }
        System.out.print(getClass().getName() + " inner in...");
        System.out.println(getClass().getEnclosingClass());
        Inner i = new Inner();
        i.show();
      }
    }
    Outer o = new Outer();
    o.show();
  }
}