CKTIS / Java
java.lang.Class — runtime type information
Class<T> represents a type in a running program. It supports object checks and casts, plus inspection of names, member declarations, generic supertypes, and annotations. This entry targets Java SE 21; the repository runtime does not attest the production runtime.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Class<String> type = String.class;
boolean matches = type.isInstance(value);
String text = type.cast(value);java.lang.Class describes a class, interface, array, primitive type, or void, not an ordinary instance of that type. The literal T.class denotes a specific permitted type, such as String.class or int.class; it cannot use a type variable T or take the form List<String>.class.
Class.getSimpleName returns the simple name; for an ordinary class, Class.getName returns its binary name: String versus java.lang.String. Class.isInstance tests object compatibility and returns false for null. Class.cast returns a compatible reference or throws ClassCastException; it returns null for null.
Class.getDeclaredFields, Class.getDeclaredMethods, and Class.getDeclaredConstructors enumerate own declarations, including nonpublic ones, excluding inherited ones; ordering is unspecified. Class.getDeclaredField looks up by name, Class.getDeclaredMethod by name and exact parameter types, and Class.getDeclaredConstructor by exact parameter types. Missing declarations cause NoSuchFieldException or NoSuchMethodException. Finding a declaration does not grant permission to use it: access checks and module restrictions still apply.
Class.getGenericSuperclass returns the direct superclass as a Type, or null for Object, interfaces, primitive types, and void. Class.getGenericInterfaces returns direct superinterfaces as a Type[]. Both methods retain generic information from declarations; they do not recover an arbitrary object's erased type arguments. Class.getClassLoader returns the loader; null can represent the bootstrap loader and is always returned for primitives and void.
Class.getAnnotationsByType reads runtime-visible annotations and expands repeatable-annotation containers. Class.isAnnotationPresent tests presence but does not expand such containers. Both follow @Inherited rules for class annotation inheritance, not inheritance from interfaces.
Examples
public class Main {
public static void main(String[] args) throws ReflectiveOperationException {
Class<String> type = String.class;
Object value = "Java";
System.out.println(type.getSimpleName());
System.out.println(type.getName());
System.out.println(type.isInstance(value));
System.out.println(type.cast(value));
System.out.println(type.isInstance(null));
System.out.println(type.cast(null));
System.out.println(type.getDeclaredConstructor().newInstance().isEmpty());
}
}Result
String java.lang.String true Java false null true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.