Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Class class

Reference for java.lang.Class: obtaining class objects, names, superclasses, interfaces, modifiers, instantiation, arrays, records and generic type erasure.

For every type the JVM loads, there is exactly one Class object that describes it: its name, superclass, interfaces, modifiers, fields, methods and constructors. Reflection starts here. Primitive types, arrays, interfaces, enums and records all have a Class object too.

You get one in three ways: a class literal (String.class), an instance (obj.getClass()), or a name (Class.forName("java.util.ArrayList")). The same Class object is returned for the same type loaded by the same class loader, so == is the right comparison.

Java
Class<?> c = "text".getClass();
System.out.println(c.getName()); // java.lang.String

Try it

Java Inspecting types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Creating objects and checking failures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class Class<T>
Member Description
Class.forName(String) Loads and initializes a class by its binary name (java.util.Map$Entry for nested types). Throws ClassNotFoundException.
getName() Binary name, e.g. java.lang.String or [I for int[].
getSimpleName() Name without package; empty for anonymous classes.
getTypeName(), getPackageName() Readable name (int[]) and package (since Java 9).
getSuperclass(), getInterfaces() Direct supertypes. Object and interfaces have no superclass (null).
isInstance(Object), isAssignableFrom(Class), cast(Object) Runtime type checks.
isInterface(), isEnum(), isRecord(), isArray(), isPrimitive(), isSealed() Kind queries.
getModifiers() Bit mask; decode with java.lang.reflect.Modifier.
getConstructor(...), getMethod(...), getField(...) Public members (including inherited methods and fields).
getDeclaredMethods(), getDeclaredFields(), ... All members declared in this class, any visibility, no inherited ones.
getRecordComponents(), getPermittedSubclasses(), getEnumConstants() Details for records, sealed types and enums.
getComponentType(), arrayType() Array element type and the array type of a class.

Usage notes

  • getClass() returns the runtime type, X.class the static one. Object o = "x"; o.getClass() is String.
  • Names: getName() on a nested class contains $ (Main$Circle); getSimpleName() does not. Arrays use encoded names such as [Ljava.lang.String;.
  • Generics are erased. List<String> and List<Integer> share one Class. Use Type and ParameterizedType or a type-token pattern to capture generics.
  • Instantiation. newInstance() on Class is deprecated since Java 9; call getDeclaredConstructor().newInstance(). Checked exceptions come wrapped in InvocationTargetException when invoked via reflection.
  • Accessing private members needs setAccessible(true) and may be blocked by the module system (InaccessibleObjectException), notably for JDK internals.
  • Cost. Reflective calls are slower than direct ones and bypass compile-time checks. Cache Method objects; consider MethodHandles in hot paths.
  • isInstance versus instanceof: the first takes a dynamic class, the second a compile-time type; null is never an instance.
  • Class literals do not initialize the class, while Class.forName(name) does (static initializers run).
  • getClass() == other.getClass() is the strict way to compare types in equals(); instanceof also accepts subclasses.