Java Reference
In-Depth Information
Constructor
is declared.
Constructor
declares various methods, including the
following methods:
•
Annotation[] getDeclaredAnnotations()
returns an array of all
annotations declared on the constructor. The returned array has zero length
when there are no annotations.
•
Class<T> getDeclaringClass()
returns a
Class
object that repres-
ents the class in which the constructor is declared.
•
Class[]<?> getExceptionTypes()
returnsanarrayof
Class
objects
representing the types of exceptions listed in the constructor's throws clause.
The returned array has zero length when there is no throws clause.
•
String getName()
returns the constructor's name.
•
Class[]<?> getParameterTypes()
returnsanarrayof
Class
objects
representing the constructor's parameters. The returned array has zero length
when the constructor does not declare parameters.
Tip
If you want to instantiate a class via a constructor that takes arguments,
you cannot use
Class
's
newInstance()
method. Instead, you must use
Con-
structor
's
T newInstance(Object... initargs)
method to perform
this task. Unlike
Class
's
newInstance()
method, which bypasses the compile-
time exception checking that would otherwise be performed by the compiler,
Con-
structor
's
newInstance()
method avoids this problem by wrapping any ex-
ception thrown by the constructor in an instance of the
java.lang.reflect.InvocationTargetException
class.
Field
representsafieldanddeclaresvariousmethods,includingthefollowingget-
ter methods:
•
Object get(Object object)
returnsthevalueofthefieldforthespe-
cified
object
.
•
boolean getBoolean(Object object)
returns the value of the
Boolean field for the specified
object
.
•
byte getByte(Object object)
returns the value of the byte integer
field for the specified
object
.
•
char getChar(Object object)
returnsthevalueofthecharacterfield
for the specified
object
.
•
double getDouble(Object object)
returns the value of the double
precision floating-point field for the specified
object
.