The Java Reflection API (java.lang.reflect together with java.lang.Class) lets a running program inspect classes, fields, methods, constructors, annotations and modules, and invoke or modify them without knowing their names at compile time. Frameworks such as Spring, Hibernate, Jackson and JUnit rely on it to wire objects, map data and discover tests.
Reflection is powerful but slower than direct calls, bypasses compile-time checks, and since Java 9 is limited by the module system. Use it for framework-style code and prefer plain Java, interfaces or method handles where you can.
Every reflective operation starts from a Class<?> object. There are three common ways to get one.
Class.forName is what enables dynamic loading, for example loading a JDBC driver or a plugin chosen from configuration.
The example below prints the structure of a small class.
getFields and getMethods return only public members, including inherited ones. getDeclaredFields and getDeclaredMethods return all members declared by that class, including private ones, but not inherited ones.
Exceptions thrown by the invoked method are wrapped in InvocationTargetException; call getCause() to see the original. Missing members throw NoSuchMethodException or NoSuchFieldException.
Annotations are only visible to reflection when they have @Retention(RetentionPolicy.RUNTIME). This is how many frameworks drive behaviour.
From Java 9 on, modules enforce strong encapsulation, and since Java 17 the JDK's internals are closed by default. Calling setAccessible(true) on a private member of a class in a package that its module does not open to you throws InaccessibleObjectException. This applies to JDK classes such as java.lang.String internals. Your own code on the classpath is unaffected, but in a named module you must declare opens com.example.model to some.framework; in module-info.java. Using --add-opens on the command line is a workaround, not a design.
Records are inspectable with Class.getRecordComponents(), and sealed hierarchies with Class.getPermittedSubclasses(), both added in recent Java releases.
setAccessible can break encapsulation and invariants.| Need | Better option |
|---|---|
| Fast repeated dynamic calls | MethodHandle and VarHandle from java.lang.invoke |
| Generating implementations of interfaces | java.lang.reflect.Proxy |
| Code generation at build time | Annotation processors |
| Plugin discovery | ServiceLoader |
getMethods returns public methods, including inherited ones. getDeclaredMethods returns all methods declared in that class, regardless of visibility, but no inherited ones.
Yes, with setAccessible(true), as long as the module system and any security restrictions allow it. It fails with InaccessibleObjectException for closed packages.
It is slower than direct invocation, but for most applications the difference is small. Cache the Method and Field objects instead of looking them up on every call.
It is deprecated. Use getDeclaredConstructor().newInstance(), which reports constructor exceptions properly.
Reflection lets code discover and use types at runtime: get a Class, inspect its members, create instances, invoke methods and read runtime annotations. Cache lookups, respect module boundaries, and choose method handles, proxies or build-time generation when reflection is not the best fit.