THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

For primitive types and void, the simple name, canonical name, and binary name are all the samethe keyword
that presents that type, such as int, float, or voidand all the name methods return the same name. For
these types, Class objects cannot be obtained from forName. You must use the class literals, such as
int.class, or the TYPE field of the appropriate wrapper class, such as Void.TYPE. If a name
representing one of these types is used with forName, it is assumed to be a user-defined class or interface
and is unlikely to be found.


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 component 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 complex 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 initializes 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 exceptions LinkageError and
ExceptionInInitializerError.


16.1.6. Runtime Type Queries


When writing your programs you can check the type of an object by using instanceof or change the type
of an expression with a cast. In both cases you need to know the name of the type involved so you can write it
in the code. When working with reflection, you have only Class objects to work with, so operators like
instanceof and casts can't be used. The class Class provides several methods that provide functionality
analogous to the language level operators:


public booleanisInstance(Object obj)

Determines if obj is assignment compatible with this class. This is the
dynamic equivalent of the instanceof operator. It returns true if obj
could be assigned to a variable of the type represented by this Class object;
and false otherwise.

public Tcast(Object obj)

Casts the object obj to the type represented by this Class object. If the cast
fails then ClassCastException is thrown. This is needed in some rare
Free download pdf