站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.1.8, Java 2 SDK 英文文档

JDK 5 Documentation v1.1.8, Java 2 SDK 英文文档

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:

 



Contents | Prev | Next
Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved.