Reflection is the ability of a program to query and modify its state during the execution.
In Java we can obtain information about the fields, methods, modifiers, and the superclass of a class at runtime.
We can also create an instance of a class whose name is not known until runtime, invoke methods on instances, and get/set its fields.
In Java we cannot change the data structure at runtime. For example, we cannot add a new field to an object at runtime.
In Java we cannot change a class's method code at runtime and we cannot add a new method to a class at runtime.
The reflection facility in Java is provided through the reflection API.
Most of the reflection API classes and interfaces are in the java.lang.reflect
package.
The Class
class, core reflection class in Java, is in the java.lang
package.
Some of the frequently used classes in reflection are listed in following table.
Class Name | Description |
---|---|
java.lang.Class | Represent a single class loaded by a class loader in the JVM. |
java.lang.reflect.Field | Represent a single field of a class or an interface. |
java.lang.reflect.Constructor | Represent a single constructor of a class. |
java.lang.reflect.Method | Represent a method of a class or an interface. |
java.lang.reflect.Modifier | Decode the access modifiers for a class and its members. |
java.lang.reflect.Array | Create arrays at runtime. |
With Java reflection we can do the following tasks.