Contents | Prev | Next | Java Core Reflection |
package java.lang.reflect;
public final class Constructor extends Object implements Member
A Constructor
provides information about, and access to, a single constructor
of a declared class. A Constructor
may be used to create and initialize a new
instance of the class that declares the reflected constructor, provided the class is
instantiable.
Only the Java Virtual Machine may create Constructor
objects; user code
obtains Constructor
references via the methods getConstructor
,
getConstructors
, getDeclaredConstructor
, and getDeclaredConstructors
of class Class
.
A Constructor
permits widening conversions to occur when matching the
actual parameters to newInstance
with the underlying constructor's formal
parameters, but it throws an IllegalArgumentException
if a narrowing
conversion would occur.
public Class getDeclaringClass()
Returns the Class
object for the class that declares the constructor represented
by this Constructor
object.
public String getName()
Returns the name of the constructor represented by this Constructor
object.
This is the same as the fully-qualified name of its declaring class.
public int getModifiers()
Returns the Java language modifiers for the constructor represented by this
Constructor
object, encoded in an integer. The Modifier
class should be used
to decode the modifiers.
The modifier encodings are defined in The Java Virtual Machine Specification, Table 4.4.
public 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.
Returns an array of length 0
if the underlying constructor takes no parameters.
public Class[] getExceptionTypes()
Returns an array of Class
objects that represent the classes of the checked
exceptions thrown by the underlying constructor represented by this
Constructor
object. Returns an array of length 0
if the constructor throws no
checked exceptions.
public boolean equals(Object obj)
Compares this Constructor
against the specified object. Returns true
if the
objects are the same; false
otherwise. Two Constructor
objects are the same
if they have the same declaring class and the same formal parameter types.
This method overrides the equals
method of class Object
.
public int hashCode()
Returns a hashcode for this Constructor
. The hashcode is the same as the
hashcode for the Constructor
's declaring class name.
This method overrides the hashCode
method of class Object
.
public String toString()
Returns a string describing the underlying constructor represented by this
Constructor
object. The string is formatted as the Java language modifiers for
the underlying constructor, if any, followed by the fully-qualified name of class
declaring the underlying constructor, followed by a parenthesized, comma-
separated list of the fully-qualified names of the constructor's formal
parameter types. If the constructor throws checked exceptions, the parameter
list is followed by a space, followed by the word throws
followed by a comma-
separated list of the fully-qualified names of the thrown exception types. For
example:
public java.util.Hashtable(int,float)The only possible modifiers for a constructor are one of
public
, protected
or
private
, or none if the constructor has default (package) access.
This method overrides the toString
method of class Object
.
Uses the constructor represented by thispublic Object newInstance(Object initargs[])
throws InstantiationException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException
Constructor
object to create and
initialize a new instance of the constructor's declaring class, with the specified
initialization parameters. Individual parameters are automatically unwrapped
to match primitive formal parameters, and both primitive and reference
parameters are subject to widening conversions as necessary. Returns the
newly created and initialized object.
Creation proceeds with the following steps, in order:
InstantiationException
.
Constructor
object enforces Java language access control and the
underlying constructor is inaccessible, the creation throws an
IllegalAccessException
.
initargs
is different from
the number of formal parameters required by the underlying constructor,
the creation throws an IllegalArgumentException
. If the underlying
constructor takes no parameters, initargs
may be null
.
initargs
array:
null
, the
conversion fails by throwing a NullPointerException
. If the object
parameter is not an instance of a standard Java wrapper class, the
conversion fails by throwing an IllegalArgumentException
.
IllegalArgumentException
.
InvocationTargetException
; newInstance
then completes abruptly by
throwing this new exception.
newInstance
.