Name

Constructor<T>

Synopsis

This class represents a constructor method of a class. Instances of Constructor are obtained by calling getConstructor( ) and related methods of java.lang.Class. Constructor implements the Member interface, so you can use the methods of that interface to obtain the constructor name, modifiers, and declaring class. In addition, getParameterTypes( ) and getExceptionTypes( ) also return important information about the represented constructor.

In addition to these methods that return information about the constructor, the newInstance( ) method allows the constructor to be invoked with an array of arguments in order to create a new instance of the class that declares the constructor. If any of the arguments to the constructor are of primitive types, they must be converted to their corresponding wrapper object types to be passed to newInstance( ). If the constructor causes an exception, the Throwable object it throws is wrapped within the InvocationTargetException that is thrown by newInstance( ). Note that newInstance( ) is much more useful than the newInstance( ) method of java.lang.Class because it can pass arguments to the constructor.

Constructor has been modified in Java 5.0 to support generics, annotations, and varargs. The changes are the same as the Java 5.0 changes to the Method class. Additionally, Constructor has been made a generic type in Java 5.0. The type variable T represents the type that the constructor constructs, and is used as the ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.