Introduction
The java.lang.reflect.Constructor class provides information about, and access to, a single constructor for a class. Constructor permits widening conversions to occur when matching the actual parameters to newInstance() with the underlying constructor’s formal parameters, but throws an IllegalArgumentException if a narrowing conversion would occur.
Class declaration
Following is the declaration for java.lang.reflect.Constructor class −
public final class Constructor<T>
extends AccessibleObject
implements GenericDeclaration, Member
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
Class methods
Sr.No. | Method & Description |
---|---|
1 | boolean equals(Object obj)Compares this Constructor against the specified object. |
2 | <T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null. |
3 | Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element. |
4 | Class<T> getDeclaringClass()Returns the Class object representing the class that declares the constructor represented by this Constructor object. |
5 | Class<?>[] getExceptionTypes()Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying constructor represented by this Constructor object. |
6 | Type[] getGenericExceptionTypes()Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object. |
7 | Type[] getGenericParameterTypes()Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object. |
8 | int getModifiers()Returns the Java language modifiers for the constructor represented by this Constructor object, as an integer. |
9 | String getName()Returns the name of this constructor, as a string. |
10 | Annotation[][] getParameterAnnotations()Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Constructor object. |
11 | Class<?>[] getParameterTypes()Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object. |
12 | int hashCode()Returns a hashcode for this Constructor. |
13 | boolean isSynthetic()Returns true if this constructor is a synthetic constructor; returns false otherwise. |
14 | boolean isVarArgs()Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise. |
15 | T newInstance(Object… initargs)Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor’s declaring class, with the specified initialization parameters. |
16 | String toGenericString()Returns a string describing this Constructor, including type parameters. |
17 | String toString()Returns a string describing this Constructor. |
Methods inherited
This class inherits methods from the following classes −
- java.lang.reflect.AccessibleObject
- java.lang.Object
Leave a Reply