---
title: "Understanding Reflection in Java: Accessing Class Information at Runtime"  
description: "Understanding Reflection in Java: Accessing Class Information at Runtime"  
author: "ICSM Computer"  
published: 2025-04-24  
updated: 2025-04-24  
canonical: https://www.mindstick.com/interview/34068/understanding-reflection-in-java-accessing-class-information-at-runtime  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Understanding Reflection in Java: Accessing Class Information at Runtime

**Reflection** in Java is a effective function that lets in programs to look at and manage **classes**, **methods**, **fields**, **and constructors** at runtime — although their names are not known until execution.

## 1. What Can You Do With Reflection?

Using reflection, you can:

1. Get information about a class (name, methods, fields, constructors, etc.)
2. Instantiate objects dynamically
3. Invoke methods dynamically
4. Access or modify fields, even private ones

## 2. How to Use Reflection

Reflection is available in the `java.lang.reflect` package. You typically start by getting the `Class` object of the target class.

```java
Class<?> clazz = Class.forName("com.example.MyClass");
```

Or, if you have the object:

```java
Class<?> clazz = myObject.getClass();
```

## 3. Examples of Using Reflection

### a. Get Class Information

```java
public class Demo {
    public int num;
    private String text;

    public void sayHello() {
        System.out.println("Hello");
    }
}

public class ReflectionInfo {
    public static void main(String[] args) {
        Class<?> clazz = Demo.class;

        System.out.println("Class Name: " + clazz.getName());

        System.out.println("\nFields:");
        for (Field field : clazz.getDeclaredFields()) {
            System.out.println(field.getName());
        }

        System.out.println("\nMethods:");
        for (Method method : clazz.getDeclaredMethods()) {
            System.out.println(method.getName());
        }
    }
}
```

### b. Accessing and Modifying Fields

```java
import java.lang.reflect.Field;

public class ReflectionFieldAccess {
    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();
        Class<?> clazz = demo.getClass();

        Field field = clazz.getDeclaredField("text");
        field.setAccessible(true); // Allow access to private field

        field.set(demo, "Reflection Rocks!");
        System.out.println("Value of text: " + field.get(demo));
    }
}
```

### c. Invoking Methods Dynamically

```java
import java.lang.reflect.Method;

public class ReflectionMethodInvoke {
    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();
        Class<?> clazz = demo.getClass();

        Method method = clazz.getDeclaredMethod("sayHello");
        method.invoke(demo); // calls demo.sayHello()
    }
}
```

### d. Creating Object Using Constructor

```java
import java.lang.reflect.Constructor;

public class ReflectionConstructor {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Demo.class;
        Constructor<?> constructor = clazz.getConstructor();
        Object obj = constructor.newInstance();

        System.out.println("Object created: " + obj);
    }
}
```

## 4. Important Notes

1. Reflection breaks **encapsulation** and should be used carefully.
2. It can lead to **performance overhead**.
3. It's frequently used in frameworks like Spring, Hibernate, and JUnit.
4. Reflection can get admission to private participants, but you need to apply setAccessible(actual).

## Answers

### Answer by ICSM Computer

**Reflection** in Java is a effective function that lets in programs to look at and manage **classes**, **methods**, **fields**, **and constructors** at runtime — although their names are not known until execution.

## 1. What Can You Do With Reflection?

Using reflection, you can:

1. Get information about a class (name, methods, fields, constructors, etc.)
2. Instantiate objects dynamically
3. Invoke methods dynamically
4. Access or modify fields, even private ones

## 2. How to Use Reflection

Reflection is available in the `java.lang.reflect` package. You typically start by getting the `Class` object of the target class.

```java
Class<?> clazz = Class.forName("com.example.MyClass");
```

Or, if you have the object:

```java
Class<?> clazz = myObject.getClass();
```

## 3. Examples of Using Reflection

### a. Get Class Information

```java
public class Demo {
    public int num;
    private String text;

    public void sayHello() {
        System.out.println("Hello");
    }
}

public class ReflectionInfo {
    public static void main(String[] args) {
        Class<?> clazz = Demo.class;

        System.out.println("Class Name: " + clazz.getName());

        System.out.println("\nFields:");
        for (Field field : clazz.getDeclaredFields()) {
            System.out.println(field.getName());
        }

        System.out.println("\nMethods:");
        for (Method method : clazz.getDeclaredMethods()) {
            System.out.println(method.getName());
        }
    }
}
```

### b. Accessing and Modifying Fields

```java
import java.lang.reflect.Field;

public class ReflectionFieldAccess {
    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();
        Class<?> clazz = demo.getClass();

        Field field = clazz.getDeclaredField("text");
        field.setAccessible(true); // Allow access to private field

        field.set(demo, "Reflection Rocks!");
        System.out.println("Value of text: " + field.get(demo));
    }
}
```

### c. Invoking Methods Dynamically

```java
import java.lang.reflect.Method;

public class ReflectionMethodInvoke {
    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();
        Class<?> clazz = demo.getClass();

        Method method = clazz.getDeclaredMethod("sayHello");
        method.invoke(demo); // calls demo.sayHello()
    }
}
```

### d. Creating Object Using Constructor

```java
import java.lang.reflect.Constructor;

public class ReflectionConstructor {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Demo.class;
        Constructor<?> constructor = clazz.getConstructor();
        Object obj = constructor.newInstance();

        System.out.println("Object created: " + obj);
    }
}
```

## 4. Important Notes

1. Reflection breaks **encapsulation** and should be used carefully.
2. It can lead to **performance overhead**.
3. It's frequently used in frameworks like Spring, Hibernate, and JUnit.
4. Reflection can get admission to private participants, but you need to apply setAccessible(actual).


---

Original Source: https://www.mindstick.com/interview/34068/understanding-reflection-in-java-accessing-class-information-at-runtime

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
