The call
java.lang.reflect.Constructor.newInstance(java.lang.Object...)
method avoids this problem by wrapping
any exception thrown by the constructor in a (checked) java.lang.reflect.InvocationTargetException
.
can be replaced by
clazz.newInstance()
The latter sequence of calls is inferred to be able to throw
the additional exception types
clazz.getDeclaredConstructor().newInstance()
InvocationTargetException
and NoSuchMethodException
. Both of these exception types are
subclasses of ReflectiveOperationException
.
Creates a new instance of the class represented by this Class
object. The class is instantiated as if by a new
expression with an empty argument list. The class is initialized if it
has not already been initialized.
IllegalAccessException | if the class or its nullary constructor is not accessible. | |
InstantiationException
| if this Class represents an abstract class,
an interface, an array class, a primitive type, or void;
or if the class has no nullary constructor;
or if the instantiation fails for some other reason. | |
ExceptionInInitializerError | if the initialization provoked by this method fails. | |
SecurityException
| If a security manager, s, is present and
the caller's class loader is not the same as or an
ancestor of the class loader for the current class and
invocation of s.checkPackageAccess() denies access to the package
of this class. |
Diagram: Class, Package, Module