6 · Advanced · lesson 40 of 41
Reflection & Annotations
Inspecting and manipulating classes at runtime.
Java
Class<?> c = Class.forName("com.acme.User");
for (var m : c.getDeclaredMethods()) {
System.out.println(m.getName());
}
// Custom annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface Benchmark { }
// Frameworks like Spring/JPA use reflection to wire your code.Key takeaways
- ✓Reflection powers frameworks — use it sparingly in app code.
- ✓Annotations attach metadata for compile-time or runtime tools.