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.

Syntax:

    public Constructor getEnclosingConstructor();

Parameter(s):

  • 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.

Example:

// 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: ");
        System.out.print(cl.getEnclosingConstructor());
    }
}

Output

Enclosing Constructor: public GetEnclosingConstructorOfClass()






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.