Java Reference
In-Depth Information
The
Class<T>
type defines a lot of methods, but I'm mentioning only a few of them here as their applic-
ation is beyond the scope of this topic. You'll probably find that the primary use you have for
Class<>
is
obtaining the class of an object by calling the
getClass()
method for the object. However, you also get a
TABLE 13-1
:
Class<T> Methods
METHOD PURPOSE
forName()
This is a static method that you can use to get the
Class<T>
object for a known class or interface
type
T
. You pass the fully qualified name of the type as a
String
object to this method, and it re-
turns the
Class<>
object for the type that has the name you have supplied (for example,
Class<String>
by specifying
forName("java.lang.String")
). If no class or interface of the
type you specify exists, a
ClassNotFoundException
exception is thrown.
newInstance()
This method calls the default constructor for the class that the current
Class<T>
object represents
and returns a new object of type
T
. When things don't work as they should, this method can throw
exceptions of type
IllegalAccessException
if class
T
or its no-arg constructor is not accessible or
of type
InstantiationException
if
T
represents an abstract class, an interface, an array type, a
primitive type, or
void
, or if
T
does not have a no-arg constructor. It can also throw an exception of
type
ExceptionInInitializerError
if the object initialization fails, or of
SecurityException
if
no permission for creating the new object exists.
getSuperclass()
This method returns a
Class<>
object for the superclass of the class represented by the current
Class<T>
object. Where the type represented is an array type, the method returns an object of type
Class<Object>
. If the current
Class<T>
object represents the
Object
class or a primitive type or
void
, then
null
is returned.
isInterface()
This method returns
true
if the current
Class<T>
object represents an interface.
getInterfaces()
This method returns an array of type
Class<?>[]
containing objects that represent the interfaces
implemented by the class or interface type corresponding to the current
Class<>
object. If the class
or interface does not implement any interfaces, the array that is returned has a length of 0.
This method returns a
String
object representing the current
Class<T>
object.
toString()
As I said, the list of methods in
Table 13-1
for the
Class<T>
type is not exhaustive. A number of other
methods defined in the class enable you to find out details of the contents of a class — the fields, the public
methods defined in the class, or even classes that are defined within the class. If you need this kind of cap-
ability, you can find out more by browsing the API documentation relating to the
Class<T>
generic type.
ARRAYS AND PARAMETERIZED TYPES
Arrays of elements that are of a type produced from a generic type are not allowed. For example, although
it looks quite reasonable, the following statement is not permitted and results in a compiler error message:
LinkedList<String>[] lists = new LinkedList<>[10]; // Will not compile!!!
Although you can declare a field in a generic type by specifying the type using a type parameter, you are
not allowed to create arrays of elements using a type parameter. For example, you can define a data member
like this:
public class MyType<T> {
// The methods and data members of the type...
private T[] data;
// This is OK