Medium👤 3-5 years👤 8-15 years 2 min read

Reflection API — Interview Questions

Asked inAmazonOracleMicrosoftDeloitte
#reflection#metaprogramming#runtime#frameworks#java
Report issue

⚡ 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.

Coffee Chat Question

Concept Made Simple

What is the Reflection API, and what are its costs and risks?

🧠Mind Map Answer

Remember It Faster

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.

InspectgetMethods / getFields / getAnnotations
InvokeMethod.invoke, Constructor.newInstance
AccesssetAccessible(true) bypasses private
CostSlower, 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.

⌨️Hands-on Keyboard

Learn by Doing

java
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);

🔥What If?

Think Beyond the Expected

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 + invocationpowers DI/ORM/JSON/test frameworkssetAccessible breaks encapsulationperformance cost vs direct callsrestricted 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

  • 1How do MethodHandles/VarHandle compare to reflection?
  • 2How does the module system restrict setAccessible?
  • 3How do dynamic proxies build on reflection?

🧩Related Technologies

SpringJacksonMethodHandlesJPMS

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions