What is reflection and how is it used
๐ก Concept: Java Reflection
Reflection in Java is a powerful API that allows a program to inspect and manipulate classes, methods, and fields at runtime.
๐ Quick Intro
Using reflection, you can examine class metadata, invoke methods dynamically, and access private members, facilitating dynamic behavior.
๐ง Analogy
Reflection is like having X-ray vision to see inside a compiled class and interact with its components dynamically.
๐ง Technical Explanation
- Reflection API includes classes like Class, Method, Field, and Constructor.
- Allows dynamic loading and inspection of classes.
- Can invoke methods and access fields at runtime.
- Used for frameworks, serialization, and debugging tools.
- Can bypass access modifiers using setAccessible(true).
๐ฏ Use Cases
- โ Developing frameworks and libraries that require dynamic behavior.
- โ Inspecting classes during runtime for debugging or testing.
- โ Serialization/deserialization and ORM frameworks.
๐ป Example: Using Reflection to Invoke a Method
import java.lang.reflect.Method;
public class ReflectionDemo {
public void sayHello() {
System.out.println("Hello from Reflection!");
}
public static void main(String[] args) throws Exception {
ReflectionDemo obj = new ReflectionDemo();
Method method = obj.getClass().getMethod("sayHello");
method.invoke(obj);
}
}

โ Interview Q&A
Q1: What is reflection in Java?
A: API for inspecting and manipulating classes at runtime.
Q2: Which classes are used in reflection?
A: Class, Method, Field, Constructor.
Q3: Can reflection access private fields?
A: Yes, with setAccessible(true).
Q4: What are common uses of reflection?
A: Frameworks, serialization, debugging.
Q5: Is reflection performance costly?
A: Yes, compared to direct calls.
Q6: Can reflection break encapsulation?
A: Potentially, if misused.
Q7: Is reflection type-safe?
A: No, it bypasses compile-time checks.
Q8: How to get class object for reflection?
A: Using Class.forName() or obj.getClass().
Q9: Can reflection create objects?
A: Yes, via Constructor class.
Q10: What are the risks of reflection?
A: Security and performance impacts.
๐ MCQs
Q1. What is reflection in Java?
- Compile time checking
- API for runtime inspection
- Memory management
- Thread management
Q2. Which classes are used in reflection?
- Thread, Process
- Class, Method, Field, Constructor
- Scanner, File
- Socket, Stream
Q3. Can reflection access private fields?
- No
- Yes
- Sometimes
- Never
Q4. What are common uses of reflection?
- I/O
- Frameworks and debugging
- Networking
- Database
Q5. Is reflection performance costly?
- No
- Yes
- Depends
- Unknown
Q6. Can reflection break encapsulation?
- No
- Yes
- Maybe
- Never
Q7. Is reflection type-safe?
- Yes
- No
- Sometimes
- Never
Q8. How to get class object for reflection?
- new Class()
- Class.forName()
- Class.get()
- Class.load()
Q9. Can reflection create objects?
- No
- Yes
- Sometimes
- Never
Q10. What are risks of reflection?
- None
- Security and performance impacts
- Only security
- Only performance
๐ก Bonus Insight
Reflection is essential for building dynamic and flexible Java frameworks but should be used carefully due to security and performance considerations.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!