Contents | Prev | Next | Java Core Reflection

# The class java.lang.reflect.Constructor

        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.

## Methods

### getDeclaringClass

        public Class getDeclaringClass()

Returns the `Class` object for the class that declares the constructor represented by this `Constructor` object.

### getName

        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.

### getModifiers

        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.

### getParameterTypes

        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.

### getExceptionTypes

        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.

### equals

        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`.

### hashCode

        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`.

### toString

        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`.

### newInstance

        public Object newInstance(Object initargs[])

            throws InstantiationException, IllegalArgumentException,

                IllegalAccessException, InvocationTargetException

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. 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:

- If the class that declares the underlying constructor represents an abstract class, the creation throws an `InstantiationException`.
- If this `Constructor` object enforces Java language access control and the underlying constructor is inaccessible, the creation throws an `IllegalAccessException`.
- If the number of actual parameters supplied via `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`.
- A new instance of the constructor's declaring class is created, and its fields are initialized to their default initial values.
- For each actual parameter in the supplied `initargs` array:
  - If the corresponding formal parameter has a primitive type, an unwrapping conversion is attempted to convert the object parameter value to a value of a primitive type. If the object parameter is `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`.
  - If, after possible unwrapping, the parameter value cannot be converted to the corresponding formal parameter type by an identity conversion or a widening conversion, the invocation throws an `IllegalArgumentException`.
- Control transfers to the underlying constructor to initialize the new instance.
- If the underlying constructor completes abruptly by throwing an exception, the exception is caught and placed in a new exception object of class `InvocationTargetException`; `newInstance` then completes abruptly by throwing this new exception.
- If the underlying constructor completes normally, the newly created and initialized instance is returned by `newInstance`.

Contents | Prev | Next

Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved.