Java Class.getEnclosingMethod()

Syntax

Class.getEnclosingMethod() has the following syntax.

public Method getEnclosingMethod()

Example

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


/*w  w w. jav a 2  s .c o  m*/
public class Main {
  public static void main(String[] args) {

    Class cls;
    cls = (new MyObject()).ClassAObject().getClass();

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

class MyObject {

  public Object c;

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

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

}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable