20.3 The Class java.lang.Class
Instances of the class Class
represent classes and interfaces in a way that can be manipulated by a running Java program. Every array also belongs to a class represented by a Class
object that is shared among all arrays with the same element type and number of dimensions.
There is no public constructor for the class Class
. The Java Virtual Machine automatically constructs Class
objects as classes are loaded; such objects cannot be created by user programs.
public final class Class {
public String toString();
public String getName();
public boolean isInterface();
public Class getSuperclass();
public Class[] getInterfaces();
public Object newInstance()
throws InstantiationException, IllegalAccessException;
public static Class forName(String className)
throws ClassNotFoundException;
public ClassLoader getClassLoader();
}
20.3.1 public String toString()
If this Class
object represents a class (which may be a declared class or an array class), a string is returned consisting of the word class
, a space, and the name of the class as returned by the getName
method (§20.3.2). If this Class
object represents an interface, a string is returned consisting of the word interface
, a space, and the name of the interface as returned by the getName
method.
In other words, this method returns a string equal to the value of:
(isInterface() ? "interface " : "class ") + getName()
Overrides the toString
method of Object
(§20.1.2).
20.3.2 public String getName()
The fully qualified name of the class or interface represented by this Class
object is returned as a String
. For example:
new Object().getClass().getName()
returns "java.lang.Object"
.
If this class object represents a class of arrays, then the name consists of the name of the element type in Java signature format, preceded by one or more "[
" characters representing the depth of array nesting. For example:
(new Object[3]).getClass().getName()
returns "[Ljava.lang.Object;"
and:
(new int[3][4][5][6][7][8][9]).getClass().getName()
returns "[[[[[[[I"
. The encoding of element type names is as follows:
B byte
C char
D double
F float
I int
J long
Lclassname; class or interface
S short
Z boolean
A class or interface name classname is given in fully qualified form as shown in the example above. For a full description of type descriptors see the chapter on the format of class files in the Java Virtual Machine Specification.
20.3.3 public boolean isInterface()
If this Class
object represents an interface, true
is returned. If this Class
object represents a class, false
is returned.
20.3.4 public Class getSuperclass()
If this Class
object represents any class other than the class Object
, then the Class
that represents the superclass of that class is returned. If this Class
object is the one that represents the class Object
, or if it represents an interface, null
is returned. If this Class
object represents an array class, then the Class
that represents class Object
is returned.
20.3.5 public Class[] getInterfaces()
This method returns an array of objects that represent interfaces. The array may be empty.
If this Class
object represents a class, the array contains objects representing all interfaces directly implemented by the class. The order of the interface objects in the array corresponds to the order of the interface names in the implements
clause of the declaration of the class represented by this Class
object. For example, given the class declaration:
class Shimmer implements FloorWax, DessertTopping { ... }
suppose the value of s
is an instance of Shimmer
; the value of the expression:
s.getClass().getInterfaces()[0]
is the Class
object that represents interface FloorWax
; and the value of:
s.getClass().getInterfaces()[1]
is the Class
object that represents interface DessertTopping
.
If this Class
object represents an interface, the array contains objects representing all interfaces directly extended by the interface-that is, the immediate superinterfaces of the interface. The order of the interface objects in the array corresponds to the order of the interface names in the extends
clause of the declaration of the interface represented by this Class
object.
20.3.6 public Object newInstance()throws InstantiationException, IllegalAccessException
This method creates and returns a new instance of the class represented by this Class
object. This is done exactly as if by a class instance creation expression (§15.8) with an empty argument list; for example, if t
is the Class
object that represents class Thread
, then t.newInstance()
does exactly the same thing as new``Thread()
. If evaluation of such a class instance creation expression would complete abruptly, then the call to the newInstance
method will complete abruptly for the same reason. See also §11.5.1.2 for more on InstantiationException
.
20.3.7 public ClassLoader getClassLoader()
This method returns a reference to the class loader (§20.14) that loaded this class. If this class has no class loader, then null
is returned.
20.3.8 public static Class forName(String className) throws ClassNotFoundException
Given the fully-qualified name of a class as a string, this method attempts to locate, load, and link the class (§12.2). If it succeeds, then a reference to the Class
object for the class is returned. If it fails, then a ClassNotFoundException
is thrown.