Java Reference
In-Depth Information
16.1.5. Obtaining Class Objects by Name
The forName method we have been using is a simpler form of the more
general forName method:
public static Class<?> forName(String name, boolean initialize,
ClassLoader loader) tHRows ClassNotFoundException
Returns the Class object associated with the named class
or interface, using the given class loader. Given the binary
name for a class or interface (in the same format returned
by getName ) or an array name in internal format, 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 loader is null , the class is loaded through the system class
loader. The class is initialized only if initialize is TRue and it
has not previously been initialized. For array types, the com-
ponent type of the array is loaded, but not initialized.
As this method description indicates, obtaining the Class object for a
class can involve loading, linking, and initializing the classa fairly com-
plex process that is described further in Section 16.13 on page 435 . The
simple Class.forName method uses the current class loaderthe one that
loaded the current class in which the forName invocation appearsand ini-
tializes the class if needed. If the class cannot be found then the checked
exception ClassNotFoundException is thrown. The exception can contain a
nested exception that describes the problem, which you can get from
invoking getCause on the exception object. This will be either the nested
exception or null . Because of the complexities of loading, linking, and
initializing a class, these methods can also throw the unchecked excep-
tions LinkageError and ExceptionInInitializerError .
 
Search WWH ::




Custom Search