Java Reference ยท Language Features by Version
Java reflection API
Reference for the Java reflection API: get Class objects, inspect names, modifiers, supertypes, records, enums and sealed types, and create instances at run time.
Reflection lets a running program look at its own structure: which fields, methods and constructors a class has, what its superclass and interfaces are, which annotations it carries, and then create objects, read fields and call methods by name instead of by compile-time reference. It lives in java.lang.Class and the java.lang.reflect package.
Reflection is the foundation of frameworks: dependency injection, serialization libraries like Jackson, test runners, ORMs and plugin systems all discover your classes this way. Application code should use it rarely, because it bypasses compile-time checking, is slower than a direct call, and is restricted by modules (JPMS). This page covers the entry point, Class; reading and calling members is in Method, Field and Constructor.
Class<?> c = "text".getClass(); // from an object Class<String> s = String.class; // class literal Class<?> d = Class.forName("java.util.ArrayList"); // from a name, may throw
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.
Getting a Class object
| Way | Example | Notes |
|---|---|---|
| Class literal | String.class, int.class, int[].class |
Checked by the compiler; no exception. |
| From an instance | obj.getClass() |
The runtime class, not the declared type. |
| By name | Class.forName("pkg.Outer$Inner") |
Binary name ($ for nested classes); throws ClassNotFoundException; initializes the class. |
| Via a loader | loader.loadClass(name) |
Does not initialize the class. |
| From another type | c.getSuperclass(), c.getComponentType(), m.getReturnType() |
Useful members of Class
| Method | Returns |
|---|---|
getName(), getSimpleName(), getTypeName(), getPackageName() |
The binary name (java.util.Map$Entry), simple name (Entry), readable type name (int[]) and package. |
getModifiers() |
Bit flags; decode with Modifier.toString(int). |
getSuperclass(), getInterfaces() |
Direct supertypes. |
isInterface(), isEnum(), isRecord(), isSealed(), isArray(), isPrimitive(), isAnnotation() |
Kind tests. |
isInstance(Object), isAssignableFrom(Class) |
Run-time equivalents of instanceof and assignment. |
getRecordComponents(), getPermittedSubclasses(), getEnumConstants() |
Details for records, sealed types and enums. |
getFields(), getMethods(), getConstructors() |
Public members, including inherited ones. |
getDeclaredFields(), getDeclaredMethods(), getDeclaredConstructors() |
All members declared by this class only, any access level. |
getDeclaredClasses(), getEnclosingClass(), getNestHost() |
Nested class information. |
getAnnotation(...) and friends |
See custom annotations. |
cast(Object) |
Checked cast to the represented type. |
Usage notes
- There is exactly one
Classobject per class per class loader, so==works for comparing them (a.getClass() == b.getClass()). - The order of
getDeclaredMethods()andgetDeclaredFields()is not specified. Sort the result by name if you print it. - Generic type arguments are erased at run time for objects (
new ArrayList<String>().getClass()is justArrayList), but declarations keep them:getGenericReturnType(),getGenericSuperclass()andgetTypeParameters()work. - Primitive types have
Classobjects too (int.class,Integer.TYPE), distinct from their wrappers. - Inner classes have binary names with
$:Outer$Inner; anonymous classesOuter$1; lambdas have synthetic hidden classes whose names you should not rely on. - Since Java 9,
setAccessible(true)on members of another named module fails unless that moduleopensthe package (modules). - For high-performance dynamic calls prefer
MethodHandle/VarHandle, or cache theMethodobjects once.