Class getEnclosingClass() method in Java with Examples Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report The getEnclosingClass() method of java.lang.Class class is used to get the enclosing class of this class. The method returns the enclosing class of this class, if this class is a local class or anonymous class declared in that class. Else this method returns null.Syntax: public Class getEnclosingClass() throws SecurityException Parameter: This method does not accept any parameter.Return Value: This method returns the enclosing class of this class if this class is a local class or anonymous class declared in that class. Else this method returns null.Exception This method throws SecurityException if a security manager is present and the security conditions are not met.Below programs demonstrate the getEnclosingClass() method.Example 1: Java // Java program to demonstrate getEnclosingClass() method import java.util.*; public class Test { public static void main(String[] args) throws ClassNotFoundException { // returns the Class object for this class Class myClass = Class.forName("Test"); System.out.println("Class represented by myClass: " + myClass.toString()); // Get the enclosing class of myClass // using getEnclosingClass() method System.out.println("EnclosingClass of myClass: " + myClass.getEnclosingClass()); } } Output: Class represented by myClass: class Test EnclosingClass of myClass: null Example 2: Java // Java program to demonstrate getEnclosingClass() method import java.util.*; class Main { public Object obj; Main() { class Arr { }; obj = new Arr(); } public static void main(String[] args) throws ClassNotFoundException { Main t = new Main(); // returns the Class object Class myClass = t.obj.getClass(); // Get the enclosing class of myClass // using getEnclosingClass() method System.out.println("EnclosingClass of myClass: " + myClass.getEnclosingClass()); } } Output: EnclosingClass of myClass: class Main Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getEnclosingClass-- Comment More infoAdvertise with us Next Article Class getEnclosingClass() method in Java with Examples S srinam Follow Improve Article Tags : Java Java-lang package Java-Functions Java.lang.Class Practice Tags : Java Similar Reads Class getDeclaringClass() method in Java with Examples The getDeclaringClass() method of java.lang.Class class is used to get the declaring class of this class. The method returns the declaring class of this class, if this class or interface is a member of another class. Else this method returns null. Syntax: public Constructor getDeclaringClass() Param 2 min read Class getEnclosingMethod() method in Java with Examples The getEnclosingMethod() method of java.lang.Class class is used to get the enclosing methods of this class. The method returns the enclosing methods of this class if this class is a local class or anonymous class declared in that method. Else this method returns null.Syntax: public Method getEnclos 2 min read Class getEnclosingConstructor() method in Java with Examples The getEnclosingConstructor() method of java.lang.Class class is used to get the enclosing constructors of this class. The method returns the enclosing constructors of this class, if this class is a local class or anonymous class declared in that constructor. Else this method returns null.Syntax: pu 2 min read Class getClasses() method in Java with Examples The getClasses() method of java.lang.Class class is used to get the classes of this class, which are the class and interfaces that are public and its members. The method returns the classes of this class in the form of array of Class objects. Syntax: public Class[] getClasses() Parameter: This metho 2 min read Class getSuperclass() method in Java with Examples The getSuperclass() method of java.lang.Class class is used to get the super class of this entity. This entity can be a class, an array, an interface, etc. The method returns the super class of this entity.Syntax: public Class<T> getSuperclass() Parameter: This method does not accept any param 2 min read Like