Java Reflection is a process of examining or modifying the run time behavior of a class at run time.
The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.
The java.lang and java.lang.reflect packages provide classes for java reflection.
The Reflection API is mainly used in:
In order to reflect a Java class, we first need to create an object of Class.
And, using the object we can call various methods to get information about methods, fields, and constructors present in a class.
There exists three ways to create objects of Class:
1. Using forName() method
class Dog {...}
// create object of Class
// to reflect the Dog class
Class a = Class.forName("Dog");
2. Using getClass() method
// create an object of Dog class
Dog d1 = new Dog();
// create an object of Class
// to reflect Dog
Class b = d1.getClass();
3. Using .class extension
// create an object of Class
// to reflect the Dog class
Class c = Dog.class;