Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Constructor

public final class Constructor
extends AccessibleObject implements GenericDeclaration, Member

java.lang.Object
   ↳ java.lang.reflect.AccessibleObject
     ↳ java.lang.reflect.Constructor<T>


This class represents a constructor. Information about the constructor can be accessed, and the constructor can be invoked dynamically.

Summary

Inherited constants

From interface java.lang.reflect.Member

Fields

protected int accessFlags

Bits encoding access (e.g.

protected Class<?> declaringClass

Method's declaring class

protected Class<?> declaringClassOfOverriddenMethod

Overriden method's declaring class (same as declaringClass unless declaringClass is a proxy class)

protected int dexMethodIndex

The method index of this method within its defining dex file

Public methods

boolean equals(Object other)

Returns true if other has the same declaring class and parameters as this constructor.

<T extends Annotation> T getAnnotation(Class<T> annotationClass)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

<A extends Annotation> A getAnnotation(Class<A> annotationType)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

Annotation[] getAnnotations()

Returns, for this element, an array containing all annotations (including inherited annotations).

Annotation[] getDeclaredAnnotations()

Returns, for this element, all annotations that are explicitly declared (not inherited).

Class<T> getDeclaringClass()

Returns the class that declares this constructor.

Class[]<?> getExceptionTypes()

Returns the exception types as an array of Class instances.

Type[] getGenericExceptionTypes()

Returns the exception types as an array of Type instances.

Type[] getGenericParameterTypes()

Returns the generic parameter types as an array of Type instances, in declaration order.

int getModifiers()

Returns the modifiers for this constructor.

String getName()

Returns the name of this constructor.

Annotation[][] getParameterAnnotations()

Returns an array of arrays that represent the annotations of the formal parameters of this constructor.

Class[]<?> getParameterTypes()

Returns an array of the Class objects associated with the parameter types of this constructor.

TypeVariable[]<Constructor<T>> getTypeParameters()

Returns the declared type parameters in declaration order.

int hashCode()

Returns an integer hash code for this object.

Equivalent to getDeclaringClass().getName().hashCode().

boolean isAnnotationPresent(Class<? extends Annotation> annotationType)

Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).

boolean isSynthetic()

Returns true if this constructor is synthetic (artificially introduced by the compiler).

boolean isVarArgs()

Returns true if this constructor takes a variable number of arguments.

T newInstance(Object... args)

Returns a new instance of the declaring class, initialized by dynamically invoking the constructor represented by this Constructor object.

void setAccessible(boolean flag)

Attempts to set the accessible flag.

String toGenericString()

Returns the string representation of the constructor's declaration, including the type parameters.

String toString()

Returns a string containing a concise, human-readable description of this constructor.

Protected methods

int compareParameters(Class[]<?> params)
boolean equalMethodParameters(Class[]<?> params)

Inherited methods

From class java.lang.reflect.AccessibleObject
From class java.lang.Object
From interface java.lang.reflect.AnnotatedElement
From interface java.lang.reflect.GenericDeclaration
From interface java.lang.reflect.Member

Fields

accessFlags

int accessFlags

Bits encoding access (e.g. public, private) as well as other runtime specific flags

declaringClass

Class<?> declaringClass

Method's declaring class

declaringClassOfOverriddenMethod

Class<?> declaringClassOfOverriddenMethod

Overriden method's declaring class (same as declaringClass unless declaringClass is a proxy class)

dexMethodIndex

int dexMethodIndex

The method index of this method within its defining dex file

Public methods

equals

Added in API level 1
boolean equals (Object other)

Returns true if other has the same declaring class and parameters as this constructor.

Parameters
other Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

getAnnotation

Added in API level 1
T getAnnotation (Class<T> annotationClass)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

Parameters
annotationClass Class: the type of the annotation to search for
Returns
T the annotation with the specified type or null

getAnnotation

Added in API level 14
A getAnnotation (Class<A> annotationType)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

Parameters
annotationType Class: the type of the annotation to search for
Returns
A the annotation with the specified type or null

getAnnotations

Added in API level 1
Annotation[] getAnnotations ()

Returns, for this element, an array containing all annotations (including inherited annotations). If there are no annotations present, this method returns a zero length array.

Returns
Annotation[] an array of all annotations for this element

getDeclaredAnnotations

Added in API level 1
Annotation[] getDeclaredAnnotations ()

Returns, for this element, all annotations that are explicitly declared (not inherited). If there are no declared annotations present, this method returns a zero length array.

Returns
Annotation[] an array of annotations declared for this element

getDeclaringClass

Added in API level 1
Class<T> getDeclaringClass ()

Returns the class that declares this constructor.

Returns
Class<T>

getExceptionTypes

Added in API level 1
Class[]<?> getExceptionTypes ()

Returns the exception types as an array of Class instances. If this constructor has no declared exceptions, an empty array will be returned.

Returns
Class[]<?>

getGenericExceptionTypes

Added in API level 1
Type[] getGenericExceptionTypes ()

Returns the exception types as an array of Type instances. If this constructor has no declared exceptions, an empty array will be returned.

Returns
Type[] an array of generic exception types
Throws
GenericSignatureFormatError if the generic constructor signature is invalid
TypeNotPresentException if any exception type points to a missing type
MalformedParameterizedTypeException if any exception type points to a type that cannot be instantiated for some reason

getGenericParameterTypes

Added in API level 1
Type[] getGenericParameterTypes ()

Returns the generic parameter types as an array of Type instances, in declaration order. If this constructor has no generic parameters, an empty array is returned.

Returns
Type[] the parameter types
Throws
GenericSignatureFormatError if the generic constructor signature is invalid
TypeNotPresentException if any parameter type points to a missing type
MalformedParameterizedTypeException if any parameter type points to a type that cannot be instantiated for some reason

getModifiers

Added in API level 1
int getModifiers ()

Returns the modifiers for this constructor. The Modifier class should be used to decode the result.

Returns
int

getName

Added in API level 1
String getName ()

Returns the name of this constructor.

Returns
String the name of this method

getParameterAnnotations

Added in API level 1
Annotation[][] getParameterAnnotations ()

Returns an array of arrays that represent the annotations of the formal parameters of this constructor. If there are no parameters on this constructor, then an empty array is returned. If there are no annotations set, then an array of empty arrays is returned.

Returns
Annotation[][] an array of arrays of Annotation instances

getParameterTypes

Added in API level 1
Class[]<?> getParameterTypes ()

Returns an array of the Class objects associated with the parameter types of this constructor. If the constructor was declared with no parameters, an empty array will be returned.

Returns
Class[]<?> the parameter types

getTypeParameters

Added in API level 1
TypeVariable[]<Constructor<T>> getTypeParameters ()

Returns the declared type parameters in declaration order. If there are no type parameters, this method returns a zero length array.

Returns
TypeVariable[]<Constructor<T>> the declared type parameters in declaration order

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Equivalent to getDeclaringClass().getName().hashCode().

Returns
int this object's hash code.

isAnnotationPresent

Added in API level 1
boolean isAnnotationPresent (Class<? extends Annotation> annotationType)

Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).

Parameters
annotationType Class: the type of the annotation to search for
Returns
boolean true if the annotation exists, false otherwise

isSynthetic

Added in API level 1
boolean isSynthetic ()

Returns true if this constructor is synthetic (artificially introduced by the compiler).

Returns
boolean

isVarArgs

Added in API level 1
boolean isVarArgs ()

Returns true if this constructor takes a variable number of arguments.

Returns
boolean

newInstance

Added in API level 1
T newInstance (Object... args)

Returns a new instance of the declaring class, initialized by dynamically invoking the constructor represented by this Constructor object. This reproduces the effect of new declaringClass(arg1, arg2, ... , argN) This method performs the following:

  • A new instance of the declaring class is created. If the declaring class cannot be instantiated (i.e. abstract class, an interface, an array type, or a primitive type) then an InstantiationException is thrown.
  • If this Constructor object is enforcing access control (see AccessibleObject) and this constructor is not accessible from the current context, an IllegalAccessException is thrown.
  • If the number of arguments passed and the number of parameters do not match, an IllegalArgumentException is thrown.
  • For each argument passed:
    • If the corresponding parameter type is a primitive type, the argument is unboxed. If the unboxing fails, an IllegalArgumentException is thrown.
    • If the resulting argument cannot be converted to the parameter type via a widening conversion, an IllegalArgumentException is thrown.
  • The constructor represented by this Constructor object is then invoked. If an exception is thrown during the invocation, it is caught and wrapped in an InvocationTargetException. This exception is then thrown. If the invocation completes normally, the newly initialized object is returned.

Parameters
args Object: the arguments to the constructor
Returns
T the new, initialized, object
Throws
InstantiationException if the class cannot be instantiated
IllegalAccessException if this constructor is not accessible
IllegalArgumentException if an incorrect number of arguments are passed, or an argument could not be converted by a widening conversion
InvocationTargetException if an exception was thrown by the invoked constructor

See also:

setAccessible

Added in API level 1
void setAccessible (boolean flag)

Attempts to set the accessible flag. Setting this to true prevents IllegalAccessExceptions.

Parameters
flag boolean

toGenericString

Added in API level 1
String toGenericString ()

Returns the string representation of the constructor's declaration, including the type parameters.

Returns
String the string representation of the constructor's declaration

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this constructor. The format of the string is:

  1. modifiers (if any)
  2. declaring class name
  3. '('
  4. parameter types, separated by ',' (if any)
  5. ')'
  6. 'throws' plus exception types, separated by ',' (if any)
For example: public String(byte[],String) throws UnsupportedEncodingException

Returns
String a printable representation for this constructor

Protected methods

compareParameters

int compareParameters (Class[]<?> params)

Parameters
params Class
Returns
int

equalMethodParameters

boolean equalMethodParameters (Class[]<?> params)

Parameters
params Class
Returns
boolean
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.