java.lang.reflect.Constructor Class in Java - GeeksforGeeks

15 Nov, 2021
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
getConstructors(): One can get all the public constructors in the respective class. it returns an array of Constructors.getDeclaredConstructors(): One can get all the constructors in the respective class irrespective of the public keyword.getName(): One can get the name of the respective constructor.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. getParameterTypes(): This returns the parameter types of a particular constructor.
getConstructors(): One can get all the public constructors in the respective class. it returns an array of Constructors.
getDeclaredConstructors(): One can get all the constructors in the respective class irrespective of the public keyword.
getName(): One can get the name of the respective constructor.
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.
 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:
Example:
Java
// Java program to show uses of Constructor class// present in java.lang.reflect package // Importing package to that examine and// introspect upon itselfimport java.lang.reflect.*; // Class 1// Helper classclass 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 classpublic 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");         // Iteerating 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();        }    }}
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 
 
adnanirshad158
singghakshay
java-lang-reflect-package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
How to remove an element from ArrayList in Java?
Difference between Abstract Class and Interface in Java