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()); }
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<out U> |
asSubclass(clazz: Class<U>)Casts this |
| T? |
Casts an object to the class or interface represented by this |
| Class<*>? |
Returns the component type of this |
| String |
Returns the descriptor string of the entity (class, interface, array class, primitive type, or |
| 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<*> |
Returns the |
| static Class<*> |
forName(name: String, initialize: Boolean, loader: ClassLoader?)Returns the |
| 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<*>? |
Returns the |
| Constructor<T> |
getConstructor(vararg parameterTypes: Class<*>!)Returns a |
| Array<Constructor<*>!> |
Returns an array containing |
| 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 |
| Constructor<T> |
getDeclaredConstructor(vararg parameterTypes: Class<*>!)Returns a |
| Array<Constructor<*>!> |
Returns an array of |
| Field |
getDeclaredField(name: String)Returns a |
| Array<Field!> |
Returns an array of |
| Method |
getDeclaredMethod(name: String, vararg parameterTypes: Class<*>!)Returns a |
| Array<Method!> |
Returns an array containing |
| Class<*>? |
If the class or interface represented by this |
| Class<*>? |
Returns the immediately enclosing class of the underlying class. |
| Constructor<*>? |
If this |
| Method? |
If this |
| Array<T>? |
Returns the elements of this enum class or null if this Class object does not represent an enum class. |
| Field |
Returns a |
| Array<Field!> |
Returns an array containing |
| Array<Type!> |
Returns the |
| Type? |
Returns the |
| Array<Class<*>!> |
Returns the interfaces directly implemented by the class or interface represented by this object. |
| Method |
Returns a |
| Array<Method!> |
Returns an array containing |
| Int |
Returns the Java language modifiers for this class or interface, encoded in an integer. |
| String |
getName()Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this |
| Class<*> |
Returns the nest host of the nest to which the class or interface represented by this |
| Array<Class<*>!> |
Returns an array containing |
| Package? |
Gets the package of this class. |
| String |
Returns the fully qualified package name. |
| Array<Class<*>!>? |
Returns an array containing |
| ProtectionDomain? |
Returns the |
| Array<RecordComponent!>? |
Returns an array of |
| URL? |
getResource(name: String)Finds a resource with a given name. |
| InputStream? |
getResourceAsStream(name: String)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 |
| String |
Return an informative string for the name of this class or interface. |
| Array<TypeVariable<Class<T>!>!> |
Returns an array of |
| Boolean |
Returns true if this |
| Boolean |
isAnnotationPresent(annotationClass: Class<out Annotation!>)Returns true if an annotation for the specified type is present on this element, else false. |
| Boolean |
Returns |
| Boolean |
isArray()Determines if this |
| Boolean |
isAssignableFrom(cls: Class<*>)Determines if the class or interface represented by this |
| Boolean |
isEnum()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 |
| Boolean |
Determines if this |
| Boolean |
Returns |
| Boolean |
Returns |
| Boolean |
isNestmateOf(c: Class<*>)Determines if the given |
| Boolean |
Determines if the specified |
| Boolean |
isRecord()Returns |
| Boolean |
isSealed()Returns |
| 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 |
| String |
Returns a string describing this |
| String |
toString()Converts the object to a string. |
Public methods
arrayType
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
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
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
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
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
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
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
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