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 thisClass object. For example, the type ofString.class is Class<String>. UseClass<?> if the class being modeled is unknown.
All Implemented Interfaces:
Serializable,Constable,TypeDescriptor,TypeDescriptor.OfField<Class<?>>,AnnotatedElement,GenericDeclaration,Type

public final classClass<T>extendsObjectimplementsSerializable,GenericDeclaration,Type,AnnotatedElement,TypeDescriptor.OfField<Class<?>>,Constable
Instances of the classClass 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 aClass 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, anddouble), and the keywordvoid are also represented asClass objects.

Class has no public constructor. Instead aClass object is constructed automatically by the Java Virtual Machine when a class is derived from the bytes of aclass file through the invocation of one of the following methods:

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

The following example uses aClass 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 theClass object for a named class or interface (or forvoid) using aclass literal. For example:
System.out.println("The name of class Foo is: "+Foo.class.getName());

Some methods of classClass expose whether the declaration of a class or interface in Java source code wasenclosed within another declaration. Other methods describe how a class or interface is situated in anest. Anest is a set of classes and interfaces, in the same run-time package, that allow mutual access to theirprivate members. The classes and interfaces are known asnestmates. One nestmate acts as thenest 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 whenclass 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 ofLookup::defineHiddenClass is ahidden 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. Thename of a hidden class or interface is not abinary 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 classClass.

Implicitly Declared Classes

Conventionally, a Java compiler, starting from a source file for an implicitly declared class, sayHelloWorld.java, creates a similarly-namedclass file,HelloWorld.class, where the class stored in thatclass file is named "HelloWorld", matching the base names of the source and class files. For theClass object of an implicitly declared class HelloWorld, the methods to get thename andtype name return results equal to"HelloWorld". Thesimple name of such an implicitly declared class is"HelloWorld" and thecanonical name is"HelloWorld".
SeeJava Language Specification:
15.8.2 Class Literals
Since:
1.0
See Also: