Reflection API & Bytecode Inspection
Use Class.forName, inspect declared methods and fields dynamically, and understand how frameworks like Spring and JUnit rely on reflection.
- Spring: Scans classes for
@Component/@Service, inspects constructors, and injects dependencies dynamically. - Hibernate / JPA: Reads
@Entityand@Columnannotations to map database rows to Java objects. - JUnit: Discovers and invokes
@Testmethods automatically. - Jackson: Serializes and deserializes POJOs/Records to JSON.
What is Reflection?
Reflection (java.lang.reflect) allows a running Java program to inspect and manipulate its own classes, methods, fields, constructors, and annotations at runtime—even without knowing their names at compile time!
How Frameworks Use Reflection
- Spring: Scans classes for
@Component/@Service, inspects constructors, and injects dependencies dynamically. - Hibernate / JPA: Reads
@Entityand@Columnannotations to map database rows to Java objects. - JUnit: Discovers and invokes
@Testmethods automatically. - Jackson: Serializes and deserializes POJOs/Records to JSON.
Trade-offs & Strong Encapsulation (Java 17+)
Reflection bypasses compile-time type safety and disables some JIT inlining optimizations. Additionally, since Java 17, calling setAccessible(true) on JDK internal classes is blocked by the module system unless explicitly opened via --add-opens.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)