Clean • Professional
Java Reflection API is a powerful feature that allows programs to inspect and manipulate classes, methods, fields, and constructors at runtime. It is widely used in modern frameworks and tools to enable dynamic behavior.
Reflection API in Java allows a program to inspect and manipulate classes, methods, fields, and constructors at runtime.
In simple words: It lets your program look inside itself and work dynamically, even if details are not known at compile time.

Reflection is widely used in real-world applications:
👉 It helps developers build flexible, reusable, and dynamic systems, which are very important in modern Java development.

Java provides the java.lang.reflect package, which contains the core classes used in Reflection. These classes help you inspect and work with different parts of a program at runtime.
Main Classes:
Class → Represents a class and provides information like class name, methods, fields, and constructorsField → Represents variables (fields) of a class and allows you to access or modify their valuesMethod → Represents methods of a class and allows you to invoke them dynamically at runtimeConstructor → Represents constructors and allows you to create objects dynamicallyReflection starts by getting the Class object, which represents the structure of a class at runtime.
class Person {}
public class Test {
public static void main(String[] args) {
Class<?> cls = Person.class;
System.out.println(cls.getName());
}
}
Explanation
Person.class → Gets the Class object of the Person classClass<?> → Represents any type of class (generic type)getName() → Returns the full class nameOutput
Person
In this step, Reflection is used to access and read a private field from a class. Normally, private fields cannot be accessed directly, but Reflection allows you to bypass this restriction.
import java.lang.reflect.Field;
class Person {
private String name = "Amit";
}
public class Test {
public static void main(String[] args) throws Exception {
Class<?> cls = Person.class;
Object obj = cls.getDeclaredConstructor().newInstance();
Field field = cls.getDeclaredField("name");
field.setAccessible(true);
System.out.println(field.get(obj));
}
}
Explanation
getDeclaredField("name") → Accesses the private field name from the classsetAccessible(true) → Bypasses access control to allow access to private fieldsfield.get(obj) → Retrieves the value of the field from the objectOutput
Amit
In this step, Reflection is used to access and execute a method dynamically at runtime. This means you can call methods without directly using them in your code.
import java.lang.reflect.Method;
class Person {
public void greet() {
System.out.println("Hello!");
}
}
public class Test {
public static void main(String[] args) throws Exception {
Class<?> cls = Person.class;
Object obj = cls.getDeclaredConstructor().newInstance();
Method method = cls.getMethod("greet");
method.invoke(obj);
}
}
Explanation
getMethod("greet") → Gets the public method greet from the classinvoke(obj) → Executes the method on the given objectOutput
Hello!
In this step, Reflection is used to load a class and create its object dynamically at runtime. This is useful when the class name is not known at compile time.
class Person {}
public class Test {
public static void main(String[] args) throws Exception {
Class<?> cls = Class.forName("Person");
Object obj = cls.getDeclaredConstructor().newInstance();
System.out.println(obj.getClass().getName());
}
}
Explanation
Class.forName("Person") → Loads the Person class at runtime using its namegetDeclaredConstructor().newInstance() → Creates a new object dynamicallyobj.getClass().getName() → Gets the class name of the created objectOutput
Person

Reflection is a powerful feature, but it is generally slower than normal Java code.
Why Reflection is Slower?
Tips to Use Reflection Efficiently
👉 Reflection should be used carefully — it is best for flexibility, not for high-performance operations.
Reflection can break Java’s built-in security and access control rules if not used carefully.
Risks
Example
field.setAccessible(true);
👉 This allows access to private variables, which are normally restricted.
Best Practice
setAccessible(true)Reflection API is a powerful feature in Java that enables runtime inspection and control of classes, methods, fields, and constructors.