Java Reflection Interview Questions and Answers

Java Reflection Interview Questions and Answers works you through Java API reflection which is used to examine or modify the behavior of methods, classes, interfaces at runtime.

1. What is Reflection in Java?
Reflection is Java language’s ability to inspect and dynamically call classes, methods, attributes etc. at Runtime. It helps in examining or modifying the Runtime behavior of a class at Runtime.
2. What are the uses of Reflection in Java?
Reflection is often used in Testing, Debugging and in Integrated Development Environment (IDE).
Reflection allows you to write programs that do not have to “know” everything at compile time. It makes programs more dynamic, since they can be tied together at runtime.
Many modern frameworks like Spring etc. use Reflection. Some modern languages like Python etc. also use Reflection.
JAVA API for XML Parsing (JAXP) also uses Reflection.
3. How can we access private method of a class from outside the class?
We can use Reflection to access private method of a class from outside the class. IN Java, we use getDeclaredMethod() to get instance of a private method. Then we mark this method accessible and finally invoke it.
In following sample code, we are accessing private method message() of class Foo by Reflection.

4. How can we create an Object dynamically at Runtime in Java?
We can use Reflection to create an Object dynamically at Runtime in Java. We can use Class.newInstance() or Constructor.newInstance() methods for creating such Objects

Leave a Comment

*

Please share it if you found this useful
Hide Buttons