Added in API level 1

Class

class Class<T : Any!> : AnnotatedElement, GenericDeclaration, Serializable, Type, TypeDescriptor.OfField<Class<*>!>
kotlin.Any
   ↳ java.lang.Class

Instances of the 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 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 are also represented as 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. A few characteristics are determined by the class loading environment at run time.

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.

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.

Summary

Public methods
Class<*>

Returns a Class for an array type whose component type is described by this Class.

Class<out U>
asSubclass(clazz: Class<U>)

Casts this Class object to represent a subclass of the class represented by the specified class object.

T?
cast(obj: Any?)

Casts an object to the class or interface represented by this Class object.

Class<*>?

Returns the component type of this Class, if it describes an array type, or null otherwise.

String

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.

static Class<*>
forName(className: String)

Returns the Class object associated with the class or interface with the given string name.

static Class<*>
forName(name: String, initialize: Boolean, loader: ClassLoader?)

Returns the Class object associated with the class or interface with the given string name, using the given class loader.

A?
getAnnotation(annotationClass: Class<A>)

Returns this element's annotation for the specified type if such an annotation is present, else null.

Array<Annotation!>

Returns annotations that are present on this element.

Array<A>
getAnnotationsByType(annotationClass: Class<A>)

Returns annotations that are associated with this element.

String?

Returns the canonical name of the underlying class as defined by The Java Language Specification.

ClassLoader?

Returns the class loader for the class.

Array<Class<*>!>

Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

Class<*>?

Returns the Class representing the component type of an array.

Constructor<T>
getConstructor(vararg parameterTypes: Class<*>!)

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

Array<Constructor<*>!>

Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

A?
getDeclaredAnnotation(annotationClass: Class<A>)

Returns this element's annotation for the specified type if such an annotation is directly present, else null.

Array<Annotation!>

Returns annotations that are directly present on this element.

Array<A>
getDeclaredAnnotationsByType(annotationClass: Class<A>)

Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

Array<Class<*>!>

Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

Constructor<T>
getDeclaredConstructor(vararg parameterTypes: Class<*>!)

Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

Array<Constructor<*>!>

Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.

Field

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Array<Field!>

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Method
getDeclaredMethod(name: String, vararg parameterTypes: Class<*>!)

Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.

Array<Method!>

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.

Class<*>?

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.

Class<*>?

Returns the immediately enclosing class of the underlying class.

Constructor<*>?

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.

Method?

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.

Array<T>?

Returns the elements of this enum class or null if this Class object does not represent an enum class.

Field

Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

Array<Field!>

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

Array<Type!>

Returns the Types representing the interfaces directly implemented by the class or interface represented by this Class object.

Type?

Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class object.

Array<Class<*>!>

Returns the interfaces directly implemented by the class or interface represented by this object.

Method
getMethod(name: String, vararg parameterTypes: Class<*>!)

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

Array<Method!>

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.

String

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

Class<*>

Returns the nest host of the nest to which the class or interface represented by this Class object belongs.

Array<Class<*>!>

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.

Package?

Gets the package of this class.

String

Returns the fully qualified package name.

Array<Class<*>!>?

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.

ProtectionDomain?

Returns the ProtectionDomain of this class.

Array<RecordComponent!>?

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.

URL?

Finds a resource with a given name.

InputStream?

Finds a resource with a given name.

Array<Any!>?

Gets the signers of this class.

String

Returns the simple name of the underlying class as given in the source code.

Class<in T>?

Returns the Class representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.

String

Return an informative string for the name of this class or interface.

Array<TypeVariable<Class<T>!>!>

Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

Boolean

Returns true if this Class object represents an annotation interface.

Boolean
isAnnotationPresent(annotationClass: Class<out Annotation!>)

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

Determines if this Class object represents an array class.

Boolean

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Boolean

Returns true if and only if this class was declared as an enum in the source code.

Boolean
isInstance(obj: Any?)

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

Returns true if and only if this class is a record class.

Boolean

Returns true if and only if this Class object represents a sealed class or interface.

Boolean

{@return {@code true} if and only if this class has the synthetic modifier * bit set}

T

Creates a new instance of the class represented by this Class object.

String

Returns a string describing this Class, including information about modifiers and type parameters.

String

Converts the object to a string.

Public methods

arrayType

Added in API level 34
fun arrayType(): Class<*>

Returns a Class for an array type whose component type is described by this Class.

Return
Class<*> a Class describing the array type

asSubclass

Added in API level 1
fun <U : Any!> asSubclass(clazz: Class<U>): Class<out U>

Casts this Class object to represent a subclass of the class represented by the specified class object. Checks that the cast is valid, and throws a ClassCastException if it is not. If this method succeeds, it always returns a reference to this Class object.

This method is useful when a client needs to "narrow" the type of a Class object to pass it to an API that restricts the Class objects that it is willing to accept. A cast would generate a compile-time warning, as the correctness of the cast could not be checked at runtime (because generic types are implemented by erasure).

Parameters
<U> the type to cast this Class object to
clazz Class<U>: the class of the type to cast this Class object to
Return
Class<out U> this Class object, cast to represent a subclass of the specified class object.
Exceptions
java.lang.ClassCastException if this Class object does not represent a subclass of the specified class (here "subclass" includes the class itself).

cast

Added in API level 1
fun cast(obj: Any?): T?

Casts an object to the class or interface represented by this Class object.

Parameters
obj Any?: the object to be cast
Return
T? the object after casting, or null if obj is null
Exceptions
java.lang.ClassCastException if the object is not null and is not assignable to the type T.

componentType

Added in API level 34
fun componentType(): Class<*>?

Returns the component type of this Class, if it describes an array type, or null otherwise.

Return
Class<*>? a Class describing the component type, or null if this Class does not describe an array type

descriptorString

Added in API level 34
fun descriptorString(): String

Returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

If this Class object represents a class or interface, not an array class, then:

  • The result is a field descriptor (JVMS {@jvms 4.3.2}) for the class or interface.

If this Class object represents an array class, then the result is a string consisting of one or more '[' characters representing the depth of the array nesting, followed by the descriptor string of the element type.

  • This array class can be described nominally.

If this Class object represents a primitive type or void, then the result is a field descriptor string which is a one-letter code corresponding to a primitive type or void ("B", "C", "D", "F", "I", "J", "S", "Z", "V") (JVMS {@jvms 4.3.2}).

Return
String the descriptor string for this Class object

desiredAssertionStatus

Added in API level 1
fun desiredAssertionStatus(): 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. If this class has had its assertion status set, the most recent setting will be returned; otherwise, if any package default assertion status pertains to this class, the most recent setting for the most specific pertinent package default assertion status is returned; otherwise, if this class is not a system class (i.e., it has a class loader) its class loader's default assertion status is returned; otherwise, the system class default assertion status is returned.

Return
Boolean the desired assertion status of the specified class.

forName

Added in API level 1
static fun forName(className: String): Class<*>

Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to: Class.forName(className, true, currentLoader) where currentLoader denotes the defining class loader of the current class.

For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread:

Class t = Class.forName("java.lang.Thread")

A call to forName("X") causes the class named X to be initialized.

Parameters
className String: the fully qualified name of the desired class.
Return
Class<*> the Class object for the class with the specified name.
Exceptions
java.lang.ClassNotFoundException if the class cannot be located
java.lang.ExceptionInInitializerError if the initialization provoked by this method fails
java.lang.LinkageError if the linkage fails

forName

Added in API level 1
static fun forName(
    name: String,
    initialize: Boolean,
    loader: ClassLoader?
): Class<*>

Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same for