Reflection
Reflection lets a running program inspect and call types, fields, and methods by name. Frameworks use it to wire beans and read annotations. Application code should use it rarely.
Detailed Explanation
Normally the compiler knows every method you call. Reflection delays that to runtime. Class.forName("shop.Gst") loads a class from a string. getDeclaredMethod("add", int.class) finds a method. invoke calls it. Fields and constructors have the same pattern. Annotations become visible with getAnnotation.
This is how Spring creates beans, how JUnit finds @Test methods, and how Jackson maps JSON keys to fields. The names live in configuration or in annotations, not in Java source the compiler can check. If you typo a name, you find out when that path runs.
Access checks still exist. private members require setAccessible(true) (or, in a module, addOpens). The JDK has been closing this door: strong encapsulation in JPMS means many setAccessible calls fail unless the module opens the package. Prefer public API, MethodHandles, or generated code (java.lang.invoke, annotation processors) when you control both sides.
Reflection is slow compared with a direct call: lookup, access checks, boxing of primitives, and no inlining until the JVM's megamorphic call site warms in a limited way. Cache Method objects if you must call in a loop. Do not look up a method by name on every request.
You reach for reflection when you are writing a framework, a mapper, or a plugin loader that cannot know the types at compile time. You avoid it for ordinary business logic. A switch on an enum, an interface, or a service loader (ServiceLoader) is safer. Tests that reach into private fields are brittle; test through the public surface.
The cost is lost static safety, harder refactoring (renames miss string names), and security: setAccessible can break encapsulation and, historically, helped exploits. In Java 9+ you will see InaccessibleObjectException. Do not disable module boundaries globally to silence it; open only the package you own.
Code Examples
import java.lang.reflect.Method;
public class CallAdd {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) throws Exception {
Method method = CallAdd.class.getDeclaredMethod("add", int.class, int.class);
Object result = method.invoke(null, 2, 3); // null receiver: static method
System.out.println(result); // prints: 5
}
}public class ClassInfo {
public static void main(String[] args) {
Class<?> type = String.class;
System.out.println(type.getName()); // prints: java.lang.String
System.out.println(type.isRecord()); // prints: false
System.out.println(Integer.class.getSuperclass().getSimpleName()); // prints: Number
}
}Common Gotchas
`getMethod` sees only public members
use `getDeclaredMethod` for package-private or private, then handle access.
`setAccessible(true)` fails across modules
export or open the package, or do not touch foreign privates.
`invoke` wraps your exception in `InvocationTargetException`
call `getCause()` to see the real failure.
Primitive arguments need `int.class`, not `Integer.class`
those are different method descriptors.