Open In App

java.lang.reflect.Constructor Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

java.lang.reflect.Constructor class is used to manage the constructor metadata like the name of the constructors, parameter types of constructors, and access modifiers of the constructors. We can inspect the constructors of classes and instantiate objects at runtime. The Constructor[] array will have one Constructor instance for each public constructor declared in the class.

In order to obtain Constructor objects, one can get the Constructor class object from the Class object.

Parameter: T- the class in which constructor is declared 

Implemented interfaces are as follows:

  • AnnotatedElement
  • GenericDeclaration
  • Member

Illustration: 

Class aClass = Employee.class;
Constructor[] constructors = aClass.getConstructors();

Pre-requisite:

Lets us do discuss some methods by which we can get information about constructors

  1. getConstructors(): One can get all the public constructors in the respective class. it returns an array of Constructors.
  2. getDeclaredConstructors(): One can get all the constructors in the respective class irrespective of the public keyword.
  3. getName(): One can get the name of the respective constructor.
  4. getModifiers(): This returns the Java language modifiers for the constructor represented by this Constructor object as an integer. The Modifier class should be used to decode the modifiers.
  5.  getParameterTypes(): This returns the parameter types of a particular constructor.

Furthermore, the primary methods of this class are given below in tabular format which is as follows:

Method Description
equals(Object obj) Compares this Constructor against the specified object.
getAnnotatedReceiverType() Returns an AnnotatedType object that represents the use of a type to specify the receiver type of the method/constructor represented by this Executable object.
getAnnotatedReturnType() Returns an AnnotatedType object that represents the use of a type to specify the return type of the method/constructor represented by this Executable.
getAnnotation(Class<T> annotationClass) Returns this element’s annotation for the specified type if such an annotation is present, else null.
getDeclaredAnnotations() Returns annotations that are directly present on this element.
getDeclaringClass() Returns the Class object representing the class or interface that declares the executable represented by this object.
getExceptionTypes() Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.
getGenericExceptionTypes() Returns an array of Type objects that represent the exceptions declared to be thrown by this executable object.
getGenericParameterTypes() Returns an array of Type objects that represent the formal parameter types, in declaration order, of the executable represented by this object.
hashcode() Return the hashcode for this constructor 
isSynthetic() Returns true if this constructor is a synthetic constructor
isVarArgs() Returns true if this constructor was declared to take a variable number of arguments 
toGenericString() Returns a string describing this constructor 
toString() Returns a string describing this constructor 

Example:

Java




// Java program to show uses of Constructor class
// present in java.lang.reflect package
 
// Importing package to that examine and
// introspect upon itself
import java.lang.reflect.*;
 
// Class 1
// Helper class
class Employee {
 
    // Member variables of this class
    int empno;
    String name;
    String address;
 
    // Constructor of this class
 
    // Constructor 1
    public Employee(int empno, String name, String address)
    {
        // 'this' keyword refers to the
        // current object itself
        this.empno = empno;
        this.name = name;
        this.address = address;
    }
 
    // Constructor 2
    public Employee(int empno, String name)
    {
 
        this.empno = empno;
        this.name = name;
    }
 
    // Constructor 3
    private Employee(String address)
    {
        this.address = address;
    }
 
    // Constructor 4
    protected Employee(int empno) { this.empno = empno; }
}
 
// Class 2
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating an object of above class
        // in the main() method
        Class c = Employee.class;
 
        // Display message
        System.out.println("All public constructor are :");
 
        Constructor[] cons = c.getConstructors();
 
        for (Constructor con : cons)
 
            // It will return all public constructor
            System.out.print(con.getName() + " ");
 
        // Display message for better readability
        System.out.println(
            "\n\nAll  constructor irrespective of access modifiers");
 
        // Getting constructors of this class
        // using getDeclaredConstructors() method
        cons = c.getDeclaredConstructors();
 
        // Iterating to print all constructors
        for (Constructor con : cons)
            System.out.print(con.getName() + " ");
 
        // Display message
        System.out.println(
            "\n\naccess modifiers of each constructor");
 
        // Iterating to get all the access modifiers
        for (Constructor con : cons)
 
            // Print all the access modifiers for all
            // constructors
            System.out.print(
                Modifier.toString(con.getModifiers())
                + " ");
 
        // Parameters types
 
        // Display message only
        System.out.println(
            "\n\ngetting parameters type of each constructor");
 
        // Iterating to get parameter types of each
        // constructor using for-each loop
        for (Constructor con : cons) {
            Class[] parameratertypes
                = con.getParameterTypes();
 
            for (Class c1 : parameratertypes) {
 
                // Print and display parameter types of all
                // constructors
                System.out.print(c1.getName() + " ");
            }
 
            // Here we are done with inner loop
            // New line
            System.out.println();
        }
    }
}


Output

All public constructor are :
Employee Employee 

All  constructor irrespective of access modifiers
Employee Employee Employee Employee 

access modifiers of each constructor
protected private public public 

getting parameters type of each constructor
int 
java.lang.String 
int java.lang.String 
int java.lang.String java.lang.String 

 



Last Updated : 05 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads