![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Class getEnclosingMethod() Method
Description
The Java Class getEnclosingMethod() method returns a Method object representing the immediately enclosing method of the underlying class, if this Class object represents a local or anonymous class within a method, else returns null.
Declaration
Following is the declaration for java.lang.Class.getEnclosingMethod() method
public Method getEnclosingMethod()
Parameters
NA
Return Value
This method returns the immediately enclosing method of the underlying class, if that class is a local or anonymous class; else null.
Exception
NA
Getting Enclosing Method of the Underlying Class Example
The following example shows the usage of java.lang.Class.getEnclosingMethod() method. In this program, we've created a Class ClassDemo. In ClassDemo constructor, a new class is created as ClassA. In main method, we've retrieved class of ClassDemo and then using getEnclosingMethod() method, enclosing method is received and printed.
package com.tutorialspoint; public class ClassDemo { public Object c; public ClassDemo() { class ClassA{}; c = new ClassA(); } public Object ClassAObject() { class ClassA{}; return new ClassA( ); } public static void main(String[] args) { Class cls; cls = (new ClassDemo()).ClassAObject().getClass(); System.out.print("Method = "); System.out.println(cls.getEnclosingMethod()); } }
Output
Let us compile and run the above program, this will produce the following result −
Method = public java.lang.Object com.tutorialspoint.ClassDemo.ClassAObject()
Getting Enclosing Method of the ArrayList Class Example
The following example shows the usage of java.lang.Class.getEnclosingMethod() method. In this program, we've used class of ArrayList. Then using getEnclosingMethod(), enclosing Method is checked and printed.
package com.tutorialspoint; import java.util.ArrayList; public class ClassDemo { public static void main(String[] args) { Class cls = ArrayList.class; System.out.println(cls.getEnclosingMethod()); } }
Output
Let us compile and run the above program, this will produce the following result −
null
Getting Enclosing Method of the Thread Class Example
The following example shows the usage of java.lang.Class.getEnclosingMethod() method. In this program, we've used class of Thread. Then using getEnclosingMethod(), enclosing Method is checked and printed.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { Class cls = Thread.class; System.out.println(cls.getEnclosingMethod()); } }
Output
Let us compile and run the above program, this will produce the following result −
null