Java.lang.Class.getEnclosingConstructor() Method

Advertisements


Description

The java.lang.Class.getEnclosingConstructor() method returns a Constructor object representing the immediately enclosing constructor of the underlying class, if this Class object represents a local or anonymous class within a constructor else returns null.

Declaration

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

public Constructor<?> getEnclosingConstructor()

Parameters

  • NA

Return Value

This method returns the immediately enclosing constructor of the underlying class, if that class is a local or anonymous class, else null.

Exception

  • NA

Example

The following example shows the usage of java.lang.Class.getEnclosingConstructor() method.

package com.tutorialspoint;

import java.lang.*;

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()).c.getClass();
         
     System.out.print("getEnclosingConstructor() = ");
     System.out.println(cls.getEnclosingConstructor());
   }
} 

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

getEnclosingConstructor() = public ClassDemo()



Advertisements
Advertisements