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
1boolean 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.
3Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.
4Class<T> getDeclaringClass()Returns the Class object representing the class that declares the constructor represented by this Constructor object.
5Class<?>[] 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.
6Type[] getGenericExceptionTypes()Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.
7Type[] getGenericParameterTypes()Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.
8int getModifiers()Returns the Java language modifiers for the constructor represented by this Constructor object, as an integer.
9String getName()Returns the name of this constructor, as a string.
10Annotation[][] 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.
11Class<?>[] getParameterTypes()Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object.
12int hashCode()Returns a hashcode for this Constructor.
13boolean isSynthetic()Returns true if this constructor is a synthetic constructor; returns false otherwise.
14boolean isVarArgs()Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise.
15T 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.
16String toGenericString()Returns a string describing this Constructor, including type parameters.
17String toString()Returns a string describing this Constructor.

Methods inherited

This class inherits methods from the following classes −

  • java.lang.reflect.AccessibleObject
  • java.lang.Object

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *