THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
Returns the Type object for the superclass of this type. This returns null if
this Class object represents the Object class, an interface, void, or
primitive type, since these have no superclass. If this Class object
represents an array, the Class object for Object is returned. By invoking
this recursively you can determine all superclasses of a class. If the
superclass is a parameterized type, the Type object will be an instance of
ParameterizedType; otherwise, the Type object will be a Class
object.

The legacy methods getInterfaces and getSuperclass are similar to the above except they return
Class objects instead of Type objects. Any parameterized type is returned as the Class object for the
corresponding raw type.


Given all the different kinds of types there are, some methods only apply to a subset of those different kinds:


public Class<?>getComponentType()

Returns the Class object representing the component type of the array
represented by this Class object. If this Class object doesn't represent an
array, null is returned. For example, given an array of int, the getClass
method will return a Class object for which isArray returns TRue and
whose getComponentType method returns the same object as
int.class.

public T[]getEnumConstants()

Returns the elements of this enum class, or null if this Class object does
not represent an enum.

public Class<?>getdeclaringClass()

Returns the Class object for the type in which this nested type was declared
as a member. If this type is not a nested member type, null is returned. For
example, when invoked on a local inner class or an anonymous inner class,
this method returns null.

public Class<?>getEnclosingClass()

Returns the Class object for the enclosing type in which this type was
declared. If this type is a top-level type, null is returned.

public Constructor<?>getEnclosingConstructor()

Returns the Constructor object (discussed shortly) for the constructor in
which this local or anonymous inner class was declared. If this isn't a local or
anonymous inner class declared in a constructor, then null is returned.

public MethodgetEnclosingMethod()

Returns the Method object (discussed shortly) for the method in which this
local or anonymous inner class was declared. If this isn't a local or
anonymous inner class declared in a method, then null is returned.

Exercise 16.1: Modify TypeDesc to skip printing anything for the Object class. It is redundant because

Free download pdf