Returns the Class
object associated with the class or
interface with the given string name, using the given class loader.
Given the fully qualified name for a class or interface (in the same
format returned by getName
) this method attempts to
locate, load, and link the class or interface. The specified class
loader is used to load the class or interface. If the parameter
loader
is null, the class is loaded through the bootstrap
class loader. The class is initialized only if the
initialize
parameter is true
and if it has
not been initialized earlier.
If name
denotes a primitive type or void, an attempt
will be made to locate a user-defined class in the unnamed package whose
name is name
. Therefore, this method cannot be used to
obtain any of the Class
objects representing primitive
types or void.
If name
denotes an array class, the component type of
the array class is loaded but not initialized.
For example, in an instance method the expression:
Class.forName("Foo")
is equivalent to:
Class.forName("Foo", true, this.getClass().getClassLoader())
Note that this method throws errors related to loading, linking or
initializing as specified in Sections 12.2, 12.3 and 12.4 of The
Java Language Specification.
Note that this method does not check whether the requested class
is accessible to its caller.
name | fully qualified name of the desired class | |
initialize | if true the class will be initialized.
See Section 12.4 of The Java Language Specification. | |
loader | class loader from which the class must be loaded |
LinkageError | if the linkage fails | |
ExceptionInInitializerError | if the initialization provoked by this method fails | |
ClassNotFoundException | if the class cannot be located by the specified class loader | |
SecurityException
| if a security manager is present, and the loader is
null , and the caller's class loader is not
null , and the caller does not have the
RuntimePermission ("getClassLoader") |
java.lang.Class.forName(String), java.lang.ClassLoader
Diagram: Class, Package, Module