Java Reflection - Java Class .getEnclosingConstructor ()








Syntax

Class.getEnclosingConstructor() has the following syntax.

public Constructor <?> getEnclosingConstructor()

Example

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

//from w ww .ja va2s  .  co m
public class Main {
  public static void main(String[] args) {

    Class cls;
    cls = (new MyClass()).c.getClass();

    System.out.print("getEnclosingConstructor() = ");
    System.out.println(cls.getEnclosingConstructor());
  }
}

class MyClass {

  public Object c;

  public MyClass() {
    class ClassA {
    }
    c = new ClassA();
  }

  public Object ClassAObject() {
    class ClassA {
    }
    return new ClassA();
  }

}

The code above generates the following result.