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.
Class<?> c = "text".getClass(); System.out.println(c.getName()); // java.lang.String
Try it
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.
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
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.classthe static one.Object o = "x"; o.getClass()isString.- 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>andList<Integer>share oneClass. UseTypeandParameterizedTypeor a type-token pattern to capture generics. - Instantiation.
newInstance()onClassis deprecated since Java 9; callgetDeclaredConstructor().newInstance(). Checked exceptions come wrapped inInvocationTargetExceptionwhen 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
Methodobjects; considerMethodHandles in hot paths. isInstanceversusinstanceof: the first takes a dynamic class, the second a compile-time type;nullis 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 inequals();instanceofalso accepts subclasses.
Related
Object,ClassLoader,Record,Enum- Lessons: Reflection, The Object class