Reflection in Java

Last Updated : 14 Mar, 2026

Reflection in Java allows a program to inspect and manipulate classes, methods, fields, and constructors at runtime, even when their details are unknown at compile time. It is provided through the java.lang.reflect package and is widely used in frameworks and libraries.

  • To examine class metadata (methods, fields, constructors) during runtime
  • To create objects and invoke methods dynamically
  • To access and modify fields or methods whose names are determined at runtime
  • To build flexible, extensible frameworks that work with unknown classes
reflection_in_java
Overview of Java Reflection – class structure, workflow, and access control

Java Reflection API

Java provides reflection through the following key classes:

  • Class
  • Constructor
  • Method
  • Field

These classes are located in the java.lang and java.lang.reflect packages.

1. Getting Class Object in Java

Before performing any reflection operation, a Class object must be obtained. The Class object represents the runtime metadata of a class, such as its methods, fields, and constructors.

Ways to Get Class Object

Java
Class<?> c1 = Test.class;

Class<?> c2 = Class.forName("Test");

Test obj = new Test();
Class<?> c3 = obj.getClass();

2. Creating Objects Using Reflection

Reflection allows objects to be created dynamically at runtime without using the new keyword. This is done by retrieving a Constructor object and invoking it programmatically.

Java
import java.lang.reflect.Constructor;

class Test {
    public Test() {
        System.out.println("Object Created");
    }
}

public class GFG {
    public static void main(String[] args) throws Exception {
        Constructor<Test> constructor = Test.class.getConstructor();
        constructor.newInstance();
    }
}

Output
Object Created

Explanation:

  • Test.class.getConstructor() retrieves the public no-argument constructor of the Test class at runtime.
  • constructor.newInstance() invokes the constructor dynamically without using the new keyword.
  • When the constructor executes, it prints "Object Created", confirming successful object creation via reflection.

3. Accessing Methods Using Reflection

Using reflection, methods of a class can be discovered and invoked at runtime by their names. This is useful when method details are not known at compile time.

Java
import java.lang.reflect.Method;

class Test {
    public void show() {
        System.out.println("Method Invoked");
    }
}

public class GFG {
    public static void main(String[] args) throws Exception {
        Test obj = new Test();
        Method m = Test.class.getMethod("show");
        m.invoke(obj);
    }
}

Output
Method Invoked

Explanation:

  • Test.class.getMethod("show") fetches the public show() method of the Test class by name.
  • m.invoke(obj) calls the retrieved method dynamically on the given object instance.
  • The method execution prints "Method Invoked", proving runtime method invocation.

4. Accessing Fields Using Reflection

Reflection provides the ability to access and modify class fields dynamically, including private fields. This is achieved using the Field class and can bypass access restrictions if required.

Java
import java.lang.reflect.Field;

class Test {
    private String msg = "Hello";
}

public class GFG {
    public static void main(String[] args) throws Exception {
        Test obj = new Test();
        Field f = Test.class.getDeclaredField("msg");
        f.setAccessible(true);
        System.out.println(f.get(obj));
    }
}

Output
Hello

Explanation:

  • getDeclaredField("msg") retrieves the private field msg from the Test class.
  • f.setAccessible(true) bypasses Java access control to allow access to the private field.
  • f.get(obj) reads the field value at runtime and prints "Hello".

Reflection vs Normal Code

Feature

Reflection

Normal Code

Compile-time Safety

No

Yes

Performance

Slower

Faster

Performance

High

Low

Security

Lower

Higher

Note:

  • Reflection is powerful but expensive
  • Avoid using it in performance-critical code
  • Prefer normal object access when possible
  • Use reflection mainly in frameworks
Comment