Text of docs.oracle.com › en › java › javase › 27 › docs › api › java.base › java › lang › Class.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Class (Java SE 27 & JDK 27)

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: ClassLoader::defineClass java.lang.invoke.MethodHandles.Lookup::defineClass java.lang.invoke.MethodHandles.Lookup::defineHiddenClass 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 (JLS §15.8.2 ).

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 (JVMS §4.7.29 ). One nestmate acts as the nest host (JVMS §4.7.28 ), 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.

Unless otherwise specified, methods in this class throw a NullPointerException when they are called with null or an array that contains null as an argument. 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 cannot be referenced by the constant pools of other classes and interfaces. A hidden class or interface cannot be described in nominal form by Class::describeConstable , ClassDesc::of , or ClassDesc::ofDescriptor . A hidden class or interface cannot be discovered by Class::forName or ClassLoader::loadClass .

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" . Since: 1.0 See Also: ClassLoader.defineClass(byte[], int, int) Serialized Form Nested Class Summary Nested classes/interfaces declared in interface TypeDescriptor TypeDescriptor.OfField <F>, TypeDescriptor.OfMethod <F, M> Modifier and Type Interface Description static interface TypeDescriptor.OfField < F extends TypeDescriptor.OfField < F >> An entity that has a field type descriptor. static interface TypeDescriptor.OfMethod < F extends TypeDescriptor.OfField < F >, M extends TypeDescriptor.OfMethod < F , M >> An entity that has a method type descriptor Method descriptors conforming to JVMS §4.3.3 can be described nominally via MethodType::describeConstable ; otherwise they cannot be described nominally.

Method Summary Modifier and Type Method Description Set < AccessFlag > accessFlags () Returns an unmodifiable set of the access flags for this class, possibly empty. Class <?> arrayType () Returns a Class for an array type whose component type is described by this Class . <U> Class <? extends U> asSubclass ( Class <U> clazz) Casts this Class object to represent a subclass of the class represented by the specified class object.

T cast ( Object obj) Casts an object to the class or interface represented by this Class object. Class <?> componentType () Returns the component type of this Class , if it describes an array type, or null otherwise. Optional < ClassDesc > describeConstable () Returns a nominal descriptor for this instance, if one can be constructed, or an empty Optional if one cannot be. String descriptorString () Returns the descriptor string of the entity (class, interface, array class, primitive type, or void ) represented by this Class object. boolean desiredAssertionStatus () Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked. static Class <?> forName ( Module module, String name) Returns the Class with the given binary name in the given module. static Class <?> forName ( String className) Returns the Class object associated with the class or interface with the given string name. static Class <?> forName ( String name, boolean initialize, ClassLoader loader) Returns the Class object associated with the class or interface with the given string name, using the given class loader. static Class <?> forPrimitiveName ( String primitiveName) Returns the Class object associated with the primitive type of the given name.

AnnotatedType [] getAnnotatedInterfaces () Returns an array of AnnotatedType objects that represent the use of types to specify superinterfaces of the entity represented by this Class object. AnnotatedType getAnnotatedSuperclass () Returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object. <A extends Annotation > A getAnnotation ( Class <A> annotationClass) Returns this element's annotation for the specified type if such an annotation is present , else null.

Annotation [] getAnnotations () Returns annotations that are present on this element. <A extends Annotation > A[] getAnnotationsByType ( Class <A> annotationClass) Returns annotations

The rest of this page is on the live site.