What is reflection in Java?
Reflection is the ability of a program to inspect and manipulate classes, methods, fields, and constructors at runtime.
What package provides reflection functionality?
java.lang.reflect.
What can reflection be used for?
Inspecting class metadata, invoking methods, accessing fields, and creating objects dynamically.
When does reflection operate?
At runtime.
Why is reflection powerful?
It allows dynamic behavior without knowing class details at compile time.
Example use of reflection?
Loading a class by name using Class.forName(“MyClass”).
Can reflection access private members?
Yes. Using setAccessible(true), though it may break encapsulation.
How does reflection help frameworks?
It allows frameworks like Spring or Hibernate to inspect classes dynamically.
Can reflection create objects?
Yes. Using Constructor.newInstance().
Can reflection call methods dynamically?
Yes. Using Method.invoke().
Does reflection affect performance?
Yes. It is slower than direct method calls.
Why is reflection slower?
Because operations are resolved dynamically rather than at compile time.
Is reflection safe to use everywhere?
No. It should be used carefully due to performance and security concerns.
What security risk does reflection pose?
It can bypass access control and modify private data.
When should reflection be used?
When dynamic behavior is required and cannot be achieved through normal code.
Key rule to remember?
Reflection = runtime inspection and dynamic invocation.
What are the main uses of reflection in Java?
Reflection is used for runtime inspection, dynamic object creation, method invocation, and framework automation.
Why is reflection useful in testing?
It allows test frameworks to access private methods and fields for validation.
How do IDEs use reflection?
IDEs use it to analyze classes, detect methods, and provide features like autocomplete and code inspection.
Why is reflection helpful for debugging?
It allows runtime inspection of object state and structure.
How do frameworks use reflection?
Frameworks like Spring and Hibernate use reflection to scan classes, inject dependencies, and map objects dynamically.
Why is reflection important for dynamic programs?
It enables programs to load and interact with classes not known at compile time.
Which Java feature supports runtime class loading?
Class.forName().
How is reflection used in plugin systems?
It loads and executes external modules dynamically.