Home » Java programming language

Java Class class getEnclosingConstructor() method with example

Class class getEnclosingConstructor() method: Here, we are going to learn about the getEnclosingConstructor() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 13, 2019

Class class getEnclosingConstructor() method

  • getEnclosingConstructor() method is available in java.lang package.
  • getEnclosingConstructor() method is used to return recent enclosing constructor of the underlying class when this Class object denotes local class inside constructor.
  • getEnclosingConstructor() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getEnclosingConstructor() method does not throw an exception at the time of returning Constructor object.


    public Constructor getEnclosingConstructor();


  • It does not accept any parameter.

Return value:

The return type of this method is Constructor, it returns recently enclosing constructor of the underlying class when this class is local or anonymous class.

Note: When this class is not local or anonymous, null is returned.


// Java program to demonstrate the example 
// of Constructor getEnclosingConstructor() method of Class 

import java.lang.reflect.*;

public class GetEnclosingConstructorOfClass {
    public Object a1;
    public GetEnclosingConstructorOfClass() {
        class A1 {};
        a1 = new A1();

    public static void main(String[] args) {
        // It returns the class of a1 object is class A1 and
        // Class A1 is enclose in public constructor of class
        // GetEnclosingConstructorOfClass class
        Class cl = (new GetEnclosingConstructorOfClass()).a1.getClass();

        System.out.print("Enclosing Constructor: ");


Enclosing Constructor: public GetEnclosingConstructorOfClass()

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.