Java Reference
In-Depth Information
Uses the constructor represented by this Constructor object to
create and initialize a new instance of the constructor's de-
claring class, with the specified initialization arguments. A ref-
erence to the newly initialized object is returned. Construct-
or.newInstance is very similar to Method.invoke . The number of
args values must equal the number of parameters for the con-
structor, and the types of those values must all be assignable
to those of the constructor. Otherwise you will get an Illeg-
alArgumentException . Again, note that for a varargs constructor
the last parameter is an array that you must fill in with the
actual "variable" arguments you want to pass. If the declaring
class is abstract you will get an InstantiationException . If the
constructor is one to which you do not have access, you will
get an IllegalAccessException . If the constructor itself throws
an exception, you will get an InvocationTargetException whose
cause is that exception.
If your constructor object is referenced through a wildcard reference,
then you must cast the object returned by newInstance to the right type.
Exercise 16.8 : Modify your Interpret program further to let users in-
voke constructors of an arbitrary class, displaying any exceptions. If a
construction is successful, let users invoke methods on the returned ob-
ject.
16.8.1. Inner Class Constructors
An inner class (excluding local and anonymous inner classes) never has
a no-arg constructor, since the compiler transforms all inner class con-
structors to take a first parameter that is a reference to the enclosing
object. This means that you can never use Class.newInstance to create an
inner class object, so you must use Constructor objects. The Construct-
or objects for an inner class reflect the transformed code, not the code
written by the programmer. For example, consider the BankAccount class
and its associated inner Action class from page 136 . The Action class had
a single constructor that took a String parameter, representing the ac-
 
Search WWH ::




Custom Search