← Back to Questions
Java

What is reflection API in Java?

Learn What is reflection API in Java? with simple explanations, real-time examples, interview tips and practical use cases.

Reflection API in Java allows a program to inspect and manipulate classes, methods, constructors, fields, and objects at runtime.

In simple words:

Reflection allows Java programs to examine and modify their own structure dynamically during execution.


Why Reflection API is Important?

Reflection is used for:

  • Framework development
  • Dependency Injection
  • ORM frameworks
  • Spring Boot internals
  • Dynamic object creation
  • Annotation processing
  • Testing frameworks

Reflection API Overview Diagram


Java Application

      |
      v

Reflection API

      |
      +-------> Inspect Class

      |
      +-------> Access Methods

      |
      +-------> Access Fields

      |
      +-------> Create Objects Dynamically


Which Package Provides Reflection API?

java.lang.reflect

Main Reflection Classes

  • Class
  • Method
  • Field
  • Constructor
  • Modifier

What is Class Class?

Every Java class has metadata represented by:

java.lang.Class

How to Get Class Object?

1. Using getClass()

Employee emp =
    new Employee();

Class c =
    emp.getClass();

2. Using .class

Class c =
    Employee.class;

3. Using Class.forName()

Class c =
    Class.forName(
        "Employee"
    );

Reflection Internal Flow


Class Loaded by JVM

      |
      v

Metadata Stored in Class Object

      |
      v

Reflection API Accesses Metadata


How to Get Class Name?

System.out.println(
    c.getName()
);

How to Get Methods?

Method[] methods =
    c.getDeclaredMethods();

Method Inspection Flow


Class Object

      |
      v

getDeclaredMethods()

      |
      v

All Methods Retrieved


How to Get Fields?

Field[] fields =
    c.getDeclaredFields();

How to Get Constructors?

Constructor[] constructors =
    c.getDeclaredConstructors();

Reflection Example

class Employee {

    private int id;

    public void show() {

        System.out.println(
            "Hello"
        );

    }

}

Accessing Methods Dynamically

Method method =
    c.getDeclaredMethod(
        "show"
    );

method.invoke(emp);

What Happens Internally?

  • Method searched dynamically
  • JVM locates method metadata
  • Method executed using invoke()

Method Invocation Flow


Method Name Provided

      |
      v

Reflection Searches Method

      |
      v

Method Metadata Found

      |
      v

invoke() Executes Method


Accessing Private Fields

Field field =
    c.getDeclaredField("id");

field.setAccessible(true);

field.set(emp, 101);

Why setAccessible(true)?

It bypasses Java access control checks.


Private Access Flow


Private Field

      |
      v

Access Restricted

      |
      v

setAccessible(true)

      |
      v

Reflection Gains Access


Creating Objects Dynamically

Object obj =
    c.newInstance();

Modern Approach

Object obj =
    c.getDeclaredConstructor()
     .newInstance();

Dynamic Object Creation Flow


Class Metadata

      |
      v

Constructor Located

      |
      v

Object Created Dynamically


How Reflection Helps Frameworks?

Frameworks use reflection for:

  • Scanning annotations
  • Creating beans
  • Dependency injection
  • Dynamic proxy generation

Spring Boot Reflection Example

@Service
class UserService {

}

What Spring Does Internally?

  • Scans class annotations
  • Creates object dynamically
  • Injects dependencies

Spring Reflection Flow


Application Starts

      |
      v

Classpath Scanned

      |
      v

@Service Detected

      |
      v

Reflection Creates Bean

      |
      v

Dependency Injected


Reflection and Annotations

Reflection is heavily used for annotation processing.


Example

if(
    c.isAnnotationPresent(
        Service.class
    )
) {

}

Reflection in Hibernate

Hibernate uses reflection for:

  • Entity scanning
  • Field mapping
  • Object population
  • Database conversion

Hibernate Flow


@Entity Detected

      |
      v

Reflection Reads Fields

      |
      v

Database Columns Mapped


Reflection in JUnit

JUnit uses reflection to:

  • Find test methods
  • Execute tests dynamically
  • Read annotations

JUnit Flow


@Test Annotation Found

      |
      v

Reflection Invokes Method

      |
      v

Test Executed


Difference Between Normal Access and Reflection

Feature Normal Access Reflection
Binding Time Compile Time Runtime
Performance Fast Slower
Dynamic Behavior Limited High
Private Access No Possible

Advantages of Reflection API

  • Dynamic programming support
  • Framework flexibility
  • Annotation processing
  • Runtime inspection
  • Useful for testing tools

Disadvantages of Reflection API

  • Slower performance
  • Security risks
  • Breaks encapsulation
  • Complex debugging

Why Reflection is Slower?

Because JVM performs runtime inspection and dynamic resolution.


Performance Flow


Reflection Request

      |
      v

Runtime Metadata Lookup

      |
      v

Dynamic Resolution

      |
      v

Method Executed


Security Risks

Reflection can access private members.


Possible Problems

  • Encapsulation violation
  • Unauthorized access
  • Security vulnerabilities

Reflection in Banking Systems

Banking applications use reflection for:

  • Framework integrations
  • Dynamic transaction mapping
  • ORM operations
  • Dependency injection

Banking Example


Transaction Entity

      |
      v

Reflection Reads Fields

      |
      v

Database Mapping Created


Reflection in E-Commerce Systems

E-commerce platforms use reflection for:

  • Product mapping
  • JSON conversion
  • Dynamic API handling
  • Dependency injection

Reflection in Microservices

Microservices architectures use reflection for:

  • REST API serialization
  • Dependency injection
  • Distributed tracing
  • Dynamic configuration loading

Jackson Reflection Flow


Incoming JSON

      |
      v

Reflection Reads DTO Fields

      |
      v

Java Object Created


Reflection and JVM

Reflection works using JVM runtime metadata stored inside Class objects.


JVM Reflection Architecture


ClassLoader

      |
      v

Class Metadata Loaded

      |
      v

Reflection API Accesses Metadata


Common Interview Mistake

Many developers think reflection is used only for private field access.

Actually:

  • Reflection is widely used in frameworks and runtime processing.

Another Common Mistake

Many developers think reflection is compile-time feature.

Actually:

  • Reflection works completely at runtime.

Best Practices

  • Use reflection only when necessary
  • Avoid excessive runtime reflection
  • Prefer normal method calls for performance-critical code
  • Secure private member access carefully
  • Cache reflection metadata if reused frequently

Realtime Enterprise Example

Spring Dependency Injection


@Service Annotation Found

      |
      v

Reflection Creates Bean

      |
      v

@Autowired Dependency Injected

      |
      v

Application Ready


Related Learning Topics


Professional Interview Answer

Reflection API in Java allows programs to inspect and manipulate classes, methods, fields, constructors, annotations, and objects dynamically at runtime. It is provided by the java.lang.reflect package and works using runtime metadata stored in Class objects by JVM. Reflection enables dynamic object creation, method invocation, private field access, annotation processing, and framework-level automation. Enterprise frameworks like Spring Boot, Hibernate, Jackson, JUnit, and modern microservices platforms heavily rely on reflection for dependency injection, ORM mapping, JSON serialization/deserialization, annotation scanning, and runtime configuration management. Although reflection provides powerful runtime flexibility, it introduces performance overhead and potential security risks if used improperly.


Frequently Asked Questions

What is Reflection API in Java?

Reflection API allows runtime inspection and manipulation of classes and objects.

Which package provides Reflection API?

java.lang.reflect package provides Reflection API.

Can reflection access private fields?

Yes, using setAccessible(true).

Why is reflection slower?

Because it performs runtime metadata lookup and dynamic resolution.

Which frameworks use reflection?

Spring, Hibernate, Jackson, and JUnit heavily use reflection.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.