java.lang.reflect.Constructor Class in Java (original) (raw)

Last Updated : 05 Dec, 2022

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, onecan get the Constructor class object from the Class object.

Parameter: T- the class in which constructor is declared

Implemented interfaces are as follows:

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(): Onecan 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(): Thisreturns 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(): Thisreturns the parameter types of a particular constructor.

Furthermore, theprimary 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 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

import java.lang.reflect.*;

class Employee {

`` int empno;

`` String name;

`` String address;

`` public Employee( int empno, String name, String address)

`` {

`` this .empno = empno;

`` this .name = name;

`` this .address = address;

`` }

`` public Employee( int empno, String name)

`` {

`` this .empno = empno;

`` this .name = name;

`` }

`` private Employee(String address)

`` {

`` this .address = address;

`` }

`` protected Employee( int empno) { this .empno = empno; }

}

public class GFG {

`` public static void main(String args[])

`` {

`` Class c = Employee. class ;

`` System.out.println( "All public constructor are :" );

`` Constructor[] cons = c.getConstructors();

`` for (Constructor con : cons)

`` System.out.print(con.getName() + " " );

`` System.out.println(

`` "\n\nAll constructor irrespective of access modifiers" );

`` cons = c.getDeclaredConstructors();

`` for (Constructor con : cons)

`` System.out.print(con.getName() + " " );

`` System.out.println(

`` "\n\naccess modifiers of each constructor" );

`` for (Constructor con : cons)

`` System.out.print(

`` Modifier.toString(con.getModifiers())

`` + " " );

`` System.out.println(

`` "\n\ngetting parameters type of each constructor" );

`` for (Constructor con : cons) {

`` Class[] parameratertypes

`` = con.getParameterTypes();

`` for (Class c1 : parameratertypes) {

`` System.out.print(c1.getName() + " " );

`` }

`` 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

Similar Reads