Java Class getEnclosingClass() Method



Description

The Java Class getEnclosingClass() method returns the immediately enclosing class of the underlying class. If this class is a top level class this method returns null.

Declaration

Following is the declaration for java.lang.Class.getEnclosingClass() method

public Class<?> getEnclosingClass()

Parameters

NA

Return Value

This method returns the immediately enclosing class of the underlying class.

Exception

NA

Getting Enclosing Class of the Underlying Class Example

The following example shows the usage of java.lang.Class.getEnclosingClass() method. In this program, we've created three classes. ClassDemo class contains two inner Classes, Outer and Inner. Outer and Inner classes have show() method which utilizes getEnclosingClass() to get the enclosing class of the underlying class. In each constructor, show method is called and result is printed.

package com.tutorialspoint;

public class ClassDemo {
   // constructor
   public ClassDemo() {

      // class Outer as inner class for class ClassDemo
      class Outer {

         public void show() {
            // inner class of Class Outer
            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 class show() function
            Inner i = new Inner();
            i.show();
         }
      }

      // outer class show() function
      Outer o = new Outer();
      o.show();
   }

   public static void main(String[] args) {
      ClassDemo cls = new ClassDemo();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

com.tutorialspoint.ClassDemo$1Outer inner in...class com.tutorialspoint.ClassDemo
com.tutorialspoint.ClassDemo$1Outer$1Inner inner in...class com.tutorialspoint.ClassDemo$1Outer

Getting Enclosing Class of the ArrayList Class Example

The following example shows the usage of java.lang.Class.getEnclosingClass() method. In this program, we've used class of ArrayList. Then using getEnclosingClass(), enclosing class 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.getEnclosingClass());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

null

Getting Enclosing Class of the Thread Class Example

The following example shows the usage of java.lang.Class.getEnclosingClass() method. In this program, we've used class of Thread. Then using getEnclosingClass(), enclosing class is checked and printed.

package com.tutorialspoint;

public class ClassDemo {
   public static void main(String[] args) {
      Class cls = Thread.class;
      
      System.out.println(cls.getEnclosingClass());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

null
java_lang_class.htm
Advertisements