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:
- 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(): Onecan get all the constructors in the respective class irrespective of the public keyword.
- getName(): One can get the name of the respective constructor.
- 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.
- 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
- java.lang.reflect.Field Class in Java The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t 5 min read
- java.lang.reflect.Proxy Class in Java A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria 4 min read
- java.lang.reflect.Method Class in Java java.lang.reflect.Method class provides necessary details about one method on a certain category or interface and provides access for the same. The reflected method could also be a category method or an instance method (including an abstract method). This class allows broadening of conversions to oc 3 min read
- java.lang.reflect.Modifier Class in Java The java.lang.reflect.Modifier class contains methods used to get information about class, member and method access modifiers. The modifiers are represented as int value with set bits at distinct positions. The int value represents different modifiers. These values are taken from the tables in secti 7 min read
- Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It 15+ min read
- java.lang.reflect.Parameter Class in Java java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter. Some useful methods of Parameter class are: public int getModifiers(): It returns the modifier flags for 4 min read
- Java.lang.Class class in Java | Set 2 Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and in 15+ min read
- Private Constructors and Singleton Classes in Java In Java, the private constructor is a special type of constructor that cannot be accessed from outside the class. This is used in conjunction with the singleton design pattern to control the instantiation. Private ConstructorThe private constructor is used to resist other classes to create new insta 2 min read
- Java.Lang.Double Class in Java Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the pr 4 min read
- Java.Lang.Long class in Java Long class is a wrapper class for the primitive type long which contains several methods to effectively deal with a long value like converting it to a string representation, and vice-versa. An object of Long class can hold a single long value. There are mainly two constructors to initialize a Long o 12 min read