← Back to Java

Java Reflection API

JavaSeniorjava

The Question

What is the Java Reflection API?

What a Strong Answer Covers

  • inspect/modify classes at runtime
  • java.lang.reflect
  • create objects dynamically
  • invoke methods at runtime
  • access private fields
  • used by Spring/Hibernate/JUnit

Senior-Level Answer

The Java Reflection API, in the `java.lang.reflect` package, enables a program to examine and modify its own structure and behavior at runtime. It allows code to inspect class metadata, invoke methods, access fields, and instantiate objects using symbolic names rather than compile-time references.

The entry point is `Class<?>`, obtained via `obj.getClass()`, `MyClass.class`, or `Class.forName("com.example.MyClass")`. From a `Class` object you can retrieve `Method`, `Field`, `Constructor`, and `Annotation` objects, then invoke methods or get/set fields even if they are private (after calling `setAccessible(true)`, subject to module and security constraints).

Common use cases: dependency injection frameworks (Spring reads annotations and injects beans), ORM tools (Hibernate maps fields to columns), serialization libraries (Jackson introspects field names), testing frameworks (JUnit discovers test methods by annotation), plugin systems (loading classes by name at runtime).

Reflection is powerful but carries real costs: 1. Performance: reflective invocation bypasses JVM optimizations (inlining, JIT hotspot compilation). Method.invoke() is measurably slower than direct calls, though `MethodHandle` (java.lang.invoke) and `invokedynamic` provide more performant alternatives. 2. Type safety: errors that would be caught at compile time (wrong argument types, missing methods) become runtime exceptions (`IllegalArgumentException`, `NoSuchMethodException`). 3. Encapsulation violation: `setAccessible(true)` overrides visibility modifiers, which was unrestricted before Java 9. Java 9+ module system (`--add-opens`) enforces access boundaries, limiting deep reflection on JDK internals and third-party modules. 4. Security: in security-managed environments, reflective access requires permissions that may not be granted.

For interviews at senior level, know that `MethodHandle` is the modern, performance-friendly alternative for dynamic dispatch, and that the module system materially changed what reflection can access.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains what reflection does, names at least three real-world use cases, and mentions performance and type-safety costs.

3/3 — Strong Answer

Also covers the Java 9 module system impact on setAccessible(), MethodHandle as a modern alternative, and can explain the specific exception hierarchy for reflective failures.

Common Mistakes

  • Presenting reflection as a general-purpose tool without acknowledging its performance and type-safety costs.
  • Not knowing that Java 9 modules restricted setAccessible() on platform classes, breaking many pre-Java 9 reflection uses.
  • Confusing java.lang.reflect.Method with java.lang.invoke.MethodHandle — the latter is faster and preferred for dynamic dispatch.

Follow-Up Questions

  • How does the Java 9 module system limit reflection, and what do you need to add to re-enable deep access? — --add-opens module/package=ALL-UNNAMED in the JVM flags, or opens declarations in module-info.java.
  • What is MethodHandle and why is it preferred over Method.invoke() for performance-sensitive reflective calls? — MethodHandle can be inlined by the JIT; Method.invoke() cannot. MethodHandle is the basis of invokedynamic.
  • How does Spring use reflection to implement dependency injection? — It scans classpath for annotated classes, reads constructor/field annotations, then calls setAccessible(true) and injects values into fields.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout