Reflection API — Interview Questions
Reviewed by Gurusankar M.
Short Answer
Reflection lets code inspect and manipulate classes, methods, fields and constructors at runtime — discovering type information and invoking members that weren't known at compile time. It powers frameworks (Spring DI, Jackson, Hibernate, JUnit) but costs performance (no inlining, access checks), breaks compile-time safety, can violate encapsulation via setAccessible, and is increasingly restricted by the module system.
On this page
Concept Made Simple
Coffee Chat Question
“What is the Reflection API, and what are its costs and risks?”
Remember It Faster
Mind Map Answer
Reflection is runtime introspection: given a Class object you can list its methods/fields, read annotations, construct instances, and call methods dynamically — all decided at runtime instead of compile time.
- Inspect
- getMethods / getFields / getAnnotations
- Invoke
- Method.invoke, Constructor.newInstance
- Access
- setAccessible(true) bypasses private
- Cost
- Slower, no compile checks, module restrictions
Every DI container, JSON mapper, ORM, and test runner leans on reflection to wire beans, map fields, and discover @Test methods — you use it indirectly all day even if you never call it directly.
Key takeaway: reflection trades compile-time safety and speed for runtime flexibility. Great for frameworks; a smell in ordinary application code where a normal call or interface would do.
Learn by Doing
Hands-on Keyboard
Class<?> clazz = Class.forName("com.app.User");
Object user = clazz.getDeclaredConstructor().newInstance();
Method setName = clazz.getMethod("setName", String.class);
setName.invoke(user, "Guru"); // dynamic call
Field id = clazz.getDeclaredField("id");
id.setAccessible(true); // bypass 'private'
id.set(user, 101);
System.out.println(user);Think Beyond the Expected
What If?
Why is reflective method invocation slower than a direct call?
Because the JIT can't resolve and inline the target the way it does for a static call site: each Method.invoke does access/argument checks, boxes primitives into an Object[], and goes through a generic dispatch path. It also blocks some optimisations. The gap has narrowed (and MethodHandles/VarHandle are faster), but in hot loops reflection is measurably costlier — so frameworks cache Method/Field objects and increasingly generate code or use MethodHandles instead.
Real World
Spring uses reflection to instantiate beans and inject dependencies; Jackson maps JSON to fields; Hibernate populates entities; JUnit finds and runs @Test methods. When you see 'InaccessibleObjectException' after upgrading Java, it's the module system blocking a framework's setAccessible — the runtime cost of reflection meeting JPMS encapsulation.
Interviewer's Expectation
Keywords they're listening for:
- runtime inspection + invocation
- powers DI/ORM/JSON/test frameworks
- setAccessible breaks encapsulation
- performance cost vs direct calls
- restricted by modules
Common Mistakes
- Using reflection where an interface/polymorphism suffices
- Not caching Method/Field objects in hot paths
- Assuming setAccessible always works under JPMS
Best Practices
- Prefer normal calls/interfaces in application code
- Cache reflective handles; consider MethodHandles for speed
- Open modules deliberately when frameworks need reflection
Follow-up Questions
- How do MethodHandles/VarHandle compare to reflection?
- How does the module system restrict setAccessible?
- How do dynamic proxies build on reflection?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Reflection (Advanced Java) Interview question: "What is the Reflection API, and what are its costs and risks?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair