Skip to content

Java Reflections

The concept of Java Reflection allows Java code to inspect and modify its own structure during runtime, providing a behind-the-scenes view of how things work. With Reflection, we can dynamically explore classes, interfaces, fields, and methods, and even interact with hidden elements. To illustrate this idea, consider a class named Person with private attributes like name, age, and occupation, alongside getter and setter methods. Typically, these private members are inaccessible from external code. However, Reflection offers a solution to this limitation, enabling us to access and modify them when needed.

java
import java.lang.reflect.Field;
import java.lang.reflect.Method;
class Person {
    private String name;
    private String age;
    private String occupation;
    // getter and setter methods
    
}
public class ReflectionDemo {
    public static void main(String[] args) throws Exception {
        // Creating an instance of the Person class
        Person person = new Person("Alice", 30, "Engineer");

        // Getting the Class object for the Person class
        Class<?> personClass = person.getClass();

        // Accessing and modifying private fields using Reflection
        Field nameField = personClass.getDeclaredField("name");
        nameField.setAccessible(true);
        nameField.set(person, "Bob");

        Field ageField = personClass.getDeclaredField("age");
        ageField.setAccessible(true);
        ageField.setInt(person, 25);

        // Invoking a private method using Reflection
        Method getOccupationMethod = personClass.getDeclaredMethod("getOccupation");
        getOccupationMethod.setAccessible(true);
        String occupation = (String) getOccupationMethod.invoke(person);

        System.out.println("Modified Person details:");
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
        System.out.println("Occupation: " + occupation);
    }
}

In this example, we create an instance of the Person class and then use Reflection to access and modify its private fields (name and age) as well as invoke a private method (getOccupation). The magic lies in the java.lang.reflect package, which provides classes and interfaces like Field, Method, and Class to achieve these feats.

Now, you might be wondering, "Why would I ever need to do such things?" Well, Reflection opens up a world of possibilities, especially in scenarios where you need to work with classes whose structure is unknown at compile time, like in frameworks, libraries, or dynamic code generation.

However, with great power comes great responsibility. It is important to use reflection cautiously, as using it too much can result in unclear code, performance issues, and potential security risks if not used wisely.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.