Module java.base
Package java.lang

Class Class<T>

java.lang.Object
java.lang.Class<T>
Type Parameters:
T - the type of the class modeled by this Class object. For example, the type of String.class is Class<String>. Use Class<?> if the class being modeled is unknown.
All Implemented Interfaces:
Serializable, Constable, TypeDescriptor, TypeDescriptor.OfField<Class<?>>, AnnotatedElement, GenericDeclaration, Type

public final class Class<T> extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement, TypeDescriptor.OfField<Class<?>>, Constable
Instances of the class Class represent classes and interfaces in a running Java application. An enum class and a record class are kinds of class; an annotation interface 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 a Class object is constructed automatically by the Java Virtual Machine when a class is derived from the bytes of a class file through the invocation of one of the following methods:

The methods of class Class expose many characteristics of a class or interface. Most characteristics are derived from the class file that the class loader passed to the Java Virtual Machine or from the class file passed to Lookup::defineClass or Lookup::defineHiddenClass. A few characteristics are determined by the class loading environment at run time, such as the module returned by getModule().

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 class or interface (or for void) using a class literal. For example:
System.out.println("The name of class Foo is: "+Foo.class.getName());

Some methods of class Class expose whether the declaration of a class or interface in Java source code was enclosed within another declaration. Other methods describe how a class or interface is situated in a nest. A nest is a set of classes and interfaces, in the same run-time package, that allow mutual access to their private members. The classes and interfaces are known as nestmates. One nestmate acts as the nest host, and enumerates the other nestmates which belong to the nest; each of them in turn records it as the nest host. The classes and interfaces which belong to a nest, including its host, are determined when class files are generated, for example, a Java compiler will typically record a top-level class as the host of a nest where the other members are the classes and interfaces whose declarations are enclosed within the top-level class declaration.

Hidden Classes

A class or interface created by the invocation of Lookup::defineHiddenClass is a hidden class or interface. All kinds of class, including enum classes and record classes, may be hidden classes; all kinds of interface, including annotation interfaces, may be hidden interfaces. The name of a hidden class or interface is not a binary name, which means the following: A hidden class or interface is never an array class, but may be the element type of an array. In all other respects, the fact that a class or interface is hidden has no bearing on the characteristics exposed by the methods of class Class.

Implicitly Declared Classes

Conventionally, a Java compiler, starting from a source file for an implicitly declared class, say HelloWorld.java, creates a similarly-named class file, HelloWorld.class, where the class stored in that class file is named "HelloWorld", matching the base names of the source and class files. For the Class object of an implicitly declared class HelloWorld, the methods to get the name and type name return results equal to "HelloWorld". The simple name of such an implicitly declared class is "HelloWorld" and the canonical name is "HelloWorld".
See Java Language Specification:
15.8.2 Class Literals
Since:
1.0
See Also: