The java.lang.reflect package contains the classes that let a running Java program inspect and manipulate classes, methods, fields, constructors, arrays and annotations at runtime. It is the core of the Reflection API (together with java.lang.Class), and it underpins frameworks for dependency injection, serialization, ORM, testing and dynamic proxies.
Reflection is powerful but slower and less safe than direct code, and since Java 9 the module system limits what it can access, so use it where static code cannot do the job.
You normally start from a Class object and navigate to its members:
| Type | Represents |
|---|---|
Field | a field; read and write its value |
Method | a method; invoke it |
Constructor | a constructor; create instances |
Parameter | a method or constructor parameter (name, type, annotations) |
Modifier | decodes modifier flags such as public or static |
RecordComponent | a component of a record (Java 16+) |
Array | creates and accesses arrays dynamically |
Proxy and InvocationHandler | dynamic proxy objects implementing interfaces |
AccessibleObject | base class that provides setAccessible |
getDeclaredXxx() returns all members declared by that class, including private ones, while getXxx() returns only public members, including inherited ones.
Use getDeclaredConstructor(...).newInstance(...). The older Class.newInstance() is deprecated, because it propagates checked exceptions without declaring them. An exception thrown inside an invoked method arrives wrapped in InvocationTargetException; call getCause() to see the real one.
setAccessible(true) lets you read, write or invoke a private member:
This works for your own code on the class path. Since Java 9, classes live in modules, and from Java 16 the JDK denies deep reflection into its own internal packages by default (the old --illegal-access option was removed in Java 17). Trying setAccessible(true) on, say, a private field of java.lang.String throws InaccessibleObjectException. To open a package to reflection in your own modules, use an opens directive in module-info.java, or --add-opens on the command line as a last resort.
Also, static final fields and fields of records and hidden classes cannot be modified through reflection, and recent Java releases are tightening the handling of mutating final fields. Treat reflection writes to final as unsupported.
Proxy creates an object at runtime that implements one or more interfaces and forwards each call to an InvocationHandler. This is how many frameworks implement logging, transactions and remote calls without writing a class per interface.
JDK proxies work only with interfaces. Libraries such as ByteBuddy create subclass proxies for classes.
Modern Java adds reflection support for new language features:
Class.getRecordComponents() lists record components, and Class.getPermittedSubclasses() lists the subtypes of a sealed class or interface. Annotations with RetentionPolicy.RUNTIME can be read using getAnnotation(...) on any AnnotatedElement, such as a Method or Field.
Reflective calls are slower than direct calls and block some compiler optimisations. Since Java 18 the core reflection implementation uses method handles internally, which narrows the gap, but cache Method and Field objects instead of looking them up repeatedly. For performance-sensitive code consider java.lang.invoke.MethodHandle, or avoid runtime lookup entirely with annotation processors and code generation. Reflection also hides errors until runtime and breaks under tools that rename or remove unused code, such as GraalVM native image, unless you register the reflective access in configuration.
getMethods() returns public methods, including inherited ones. getDeclaredMethods() returns every method declared in that class only, regardless of visibility, but not inherited ones.
The target package is not open to your module. Add an opens directive for that package, or --add-opens module/package=ALL-UNNAMED for code on the class path. JDK internals are closed by default.
Yes, frameworks depend on it. Keep it out of hot loops, cache lookups, and validate names that come from external input, since reflective calls can reach members you did not intend to expose.
MethodHandle and VarHandle from java.lang.invoke, or compile-time code generation.
java.lang.reflect gives runtime access to a program's structure: fields, methods, constructors, parameters, arrays and dynamic proxies. Combined with Class, it powers frameworks and tools, now with support for records and sealed types. Respect the module system, cache your lookups, handle InvocationTargetException, and prefer static code or method handles when the structure is known at compile time.