java.lang.reflect.Constructor Class in Java
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:
Class aClass = Employee.class; Constructor constructors = aClass.getConstructors();
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.
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
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