Java FAQ: Top Questions
39. What is reflection in Java?
Reflection
in Java allows inspection and modification of classes, methods, fields, and constructors at runtime, provided by the
java.lang.reflect
package.
- Capabilities: Access private members, invoke methods, create instances dynamically.
-
Classes:
Class
,Method
,Field
,Constructor
. - Performance: Slower than direct access due to runtime checks.
- Risks: Breaks encapsulation, may cause security issues.
- Use Case: Frameworks (e.g., Spring), testing tools, debuggers.