What is Java Reflection?
Answer
Reflection is the ability of a program to examine and modify its own structure and behavior at runtime. Through the java.lang.reflect package, you can inspect classes (Class.forName("com.example.MyClass")), list fields and methods, invoke methods dynamically, and even access private members. Reflection is used by frameworks like Spring (dependency injection, annotation processing), Hibernate (ORM mapping), and JUnit (test discovery). It bypasses compile-time type checking, which makes it powerful but risky — improper use leads to security vulnerabilities and performance overhead. Reflection is generally a tool for framework authors, not application code.
Previous
What is the difference between Comparable and Comparator?
Next
What is the difference between List.of() and Arrays.asList()?
More Java Questions
View all →- Intermediate What is the Java Collections Framework?
- Intermediate What is the difference between ArrayList and LinkedList?
- Intermediate What is HashMap in Java and how does it work internally?
- Intermediate What is the difference between HashMap and HashTable in Java?
- Intermediate What is the difference between HashMap and LinkedHashMap?