T - the type of the class modeled by this Class
object. For example, the type of String.class is Class. Use Class> if the class being modeled is
unknown.
Instances of the class Class represent classes and
interfaces in a running Java application. An enum is a kind of
class and an annotation is a kind of interface. Every array also
belongs to a class that is reflected as a Class object
that is shared by all arrays with the same element type and number
of dimensions. The primitive Java types (boolean,
byte, char, short,
int, long, float, and
double), and the keyword void are also
represented as Class objects.
Class has no public constructor. Instead Class
objects are constructed automatically by the Java Virtual Machine as classes
are loaded and by calls to the defineClass method in the class
loader.
The following example uses a Class object to print the
class name of an object:
void printClassName(Object obj) {
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}
It is also possible to get the Class object for a named
type (or for void) using a class literal
(JLS Section 15.8.2).
For example:
System.out.println("The name of class Foo is: "+Foo.class.getName());
Classrepresent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as aClassobject that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean,byte,char,short,int,long,float, anddouble), and the keywordvoidare also represented asClassobjects.Classhas no public constructor. InsteadClassobjects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to thedefineClassmethod in the class loader.The following example uses a
Classobject to print the class name of an object:It is also possible to get the
Classobject for a named type (or for void) using a class literal (JLS Section 15.8.2). For example: