Reflection in Java is the ability of a running program to inspect and manipulate its own structure: classes, interfaces, fields, methods, constructors and annotations. The API lives in java.lang.reflect and java.lang.Class. You can load a class by name, read its members, create instances and invoke methods without knowing the types at compile time, which is how frameworks such as Spring, Hibernate and JUnit work.
Reflection is powerful but slower than direct calls, bypasses compile-time checks and is restricted by the module system. This article shows the main operations, current access rules and the alternatives to prefer.
Every type has a Class<?> object that describes it. From there you reach the other reflective types.
| Type | Represents | Obtained with |
|---|---|---|
Class<?> | A class, interface, enum, record or array | Foo.class, obj.getClass(), Class.forName("pkg.Foo") |
Field | A field | getDeclaredField(name) |
Method | A method | getDeclaredMethod(name, paramTypes...) |
Constructor<?> | A constructor | getDeclaredConstructor(paramTypes...) |
RecordComponent | A record component | getRecordComponents() |
Parameter | A method parameter | method.getParameters() |
Methods named getX return only public members, including inherited ones. Methods named getDeclaredX return members declared by that class at any access level, but not inherited ones.
This example lists the public methods of a class and its modifiers using only the class name.
Class.forName throws ClassNotFoundException, a checked exception, so declare or handle it. Catching Exception and calling printStackTrace() hides real errors, so handle each failure meaningfully.
Use getDeclaredConstructor().newInstance() to create an object and Method.invoke() to call a method. The older Class.newInstance() is deprecated.
ReflectiveOperationException is the common parent of ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException and InvocationTargetException. If the invoked method throws, you receive an InvocationTargetException and must call getCause() to see the real exception.
setAccessible(true) lets reflection read or write private fields and call private methods:
This works for your own classes on the class path. Since Java 9 the module system limits deep reflection: code in another module can reach a private member only if the package is opened to it with opens in module-info.java or the --add-opens option. Since Java 16 the JDK internals are strongly encapsulated by default, so reflecting into java.base private members throws InaccessibleObjectException. Final fields of records and hidden classes cannot be changed through reflection.
Frameworks read annotations through reflection. The annotation must have RUNTIME retention.
Reflection has real trade-offs:
Method and Field objects instead of looking them up each time.Prefer these options when you can: MethodHandles and VarHandle (java.lang.invoke) for faster, type-safer dynamic access, annotation processors for compile-time code generation, and ServiceLoader for plug-ins.
It powers dependency injection, serialization libraries, ORMs, test frameworks and annotation-driven tools, where code must work with types it does not know at compile time.
It is slower than a direct call, but the cost is small for most applications. Cache lookups and avoid reflection in tight loops.
It can read private members after setAccessible(true), but the module system and strong encapsulation of the JDK restrict this in modern Java.
Class.newInstance()?Use clazz.getDeclaredConstructor().newInstance(), which reports constructor exceptions correctly.
Java reflection lets code discover and use classes, fields, methods and annotations at runtime through Class and java.lang.reflect. Use getDeclared... for members of one class, handle InvocationTargetException, and respect module boundaries. Keep reflection out of hot paths and prefer method handles or generated code when you control both sides.