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.
If the loader
is null
, and a security
manager is present, and the caller's class loader is not null, then this
method calls the security manager's checkPermission
method
with a RuntimePermission("getClassLoader")
permission to
ensure it's ok to access the bootstrap class loader.
Returns:
class object representing the desired class
Parameters:
- name - fully qualified name of the desired class
- initialize - whether the class must be initialized
- loader - class loader from which the class must be loaded
Throws:
- 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
Since:
1.2
See Also:
Class.forName(String)
,
ClassLoader
,