Class (Java SE 17 & JDK 17) (original) (raw)

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](../io/Serializable.html "interface in java.io"), [Constable](constant/Constable.html "interface in java.lang.constant"), [TypeDescriptor](invoke/TypeDescriptor.html "interface in java.lang.invoke"), [TypeDescriptor.OfField](invoke/TypeDescriptor.OfField.html "interface in java.lang.invoke")<[Class](Class.html "class in java.lang")<?>>, [AnnotatedElement](reflect/AnnotatedElement.html "interface in java.lang.reflect"), [GenericDeclaration](reflect/GenericDeclaration.html "interface in java.lang.reflect"), [Type](reflect/Type.html "interface in java.lang.reflect")


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 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.

A class or interface created by the invocation ofLookup::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.

See Java Language Specification:

15.8.2 Class Literals

Since:

1.0

See Also:

[arrayType](#arrayType%28%29)()
Returns a Class for an array type whose component type is described by this Class.
<U> [Class](Class.html "class in java.lang")<? extends U>
Casts this Class object to represent a subclass of the class represented by the specified class object.
Casts an object to the class or interface represented by this Class object.
Returns the component type of this Class, if it describes an array type, or null otherwise.
Returns a nominal descriptor for this instance, if one can be constructed, or an empty Optional if one cannot be.
Returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.
boolean
Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
Returns the Class with the given binary name in the given module.
Returns the Class object associated with the class or interface with the given string name.
Returns the Class object associated with the class or interface with the given string name, using the given class loader.
Returns an array of AnnotatedType objects that represent the use of types to specify superinterfaces of the entity represented by thisClass object.
Returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.
[getAnnotation](#getAnnotation%28java.lang.Class%29)([Class](Class.html "class in java.lang")<A> annotationClass)
Returns this element's annotation for the specified type if such an annotation is present, else null.
Returns annotations that are present on this element.
[getAnnotationsByType](#getAnnotationsByType%28java.lang.Class%29)([Class](Class.html "class in java.lang")<A> annotationClass)
Returns annotations that are associated with this element.
Returns the canonical name of the underlying class as defined by The Java Language Specification.
Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.
Returns the class loader for the class.
Returns the Class representing the component type of an array.
[getConstructor](#getConstructor%28java.lang.Class...%29)([Class](Class.html "class in java.lang")<?>... parameterTypes)
Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by thisClass object.
[getDeclaredAnnotation](#getDeclaredAnnotation%28java.lang.Class%29)([Class](Class.html "class in java.lang")<A> annotationClass)
Returns this element's annotation for the specified type if such an annotation is directly present, else null.
Returns annotations that are directly present on this element.
[getDeclaredAnnotationsByType](#getDeclaredAnnotationsByType%28java.lang.Class%29)([Class](Class.html "class in java.lang")<A> annotationClass)
Returns this element's annotation(s) for the specified type if such annotations are either directly present or_indirectly present_.
Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
[getDeclaredConstructor](#getDeclaredConstructor%28java.lang.Class...%29)([Class](Class.html "class in java.lang")<?>... parameterTypes)
Returns a Constructor object that reflects the specified constructor of the class or interface represented by thisClass object.
Returns an array of Constructor objects reflecting all the constructors declared by the class represented by thisClass object.
Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by thisClass object.
Returns a Method object that reflects the specified declared method of the class or interface represented by thisClass object.
Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.
If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.
Returns the immediately enclosing class of the underlying class.
If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class.
If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.
Returns the elements of this enum class or null if this Class object does not represent an enum class.
Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.
[getFields](#getFields%28%29)()
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
Returns the Types representing the interfaces directly implemented by the class or interface represented by this Class object.
Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class object.
Returns the interfaces directly implemented by the class or interface represented by this Class object.
Returns a Method object that reflects the specified public member method of the class or interface represented by thisClass object.
Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.
int
Returns the Java language modifiers for this class or interface, encoded in an integer.
[getModule](#getModule%28%29)()
Returns the module that this class or interface is a member of.
[getName](#getName%28%29)()
Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object.
Returns the nest host of the nest to which the class or interface represented by this Class object belongs.
Returns an array containing Class objects representing all the classes and interfaces that are members of the nest to which the class or interface represented by this Class object belongs.
Gets the package of this class.
Returns the fully qualified package name.
Returns an array containing Class objects representing the direct subinterfaces or subclasses permitted to extend or implement this class or interface if it is sealed.
Returns the ProtectionDomain of this class.
Returns an array of RecordComponent objects representing all the record components of this record class, or null if this class is not a record class.
Finds a resource with a given name.
Finds a resource with a given name.
Gets the signers of this class.
Returns the simple name of the underlying class as given in the source code.
Returns the Class representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.
Return an informative string for the name of this class or interface.
Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by thisGenericDeclaration object, in declaration order.
boolean
Returns true if this Class object represents an annotation interface.
boolean
Returns true if an annotation for the specified type is present on this element, else false.
boolean
Returns true if and only if the underlying class is an anonymous class.
boolean
[isArray](#isArray%28%29)()
Determines if this Class object represents an array class.
boolean
Determines if the class or interface represented by thisClass object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specifiedClass parameter.
boolean
[isEnum](#isEnum%28%29)()
Returns true if and only if this class was declared as an enum in the source code.
boolean
[isHidden](#isHidden%28%29)()
Returns true if and only if the underlying class is a hidden class.
boolean
Determines if the specified Object is assignment-compatible with the object represented by this Class.
boolean
Determines if this Class object represents an interface type.
boolean
Returns true if and only if the underlying class is a local class.
boolean
Returns true if and only if the underlying class is a member class.
boolean
Determines if the given Class is a nestmate of the class or interface represented by this Class object.
boolean
Determines if the specified Class object represents a primitive type.
boolean
[isRecord](#isRecord%28%29)()
Returns true if and only if this class is a record class.
boolean
[isSealed](#isSealed%28%29)()
Returns true if and only if this Class object represents a sealed class or interface.
boolean
Returns true if and only if this class has the synthetic modifier bit set.
Returns a string describing this Class, including information about modifiers and type parameters.
[toString](#toString%28%29)()
Converts the object to a string.