Reflection API

Last Updated : 21 Mar 2026

Java Reflection allows a program to inspect and modify the behavior of classes at runtime.

In this chapter, you will learn about the Reflection API in Java, its key classes, and how it works at runtime.

What is Java Reflection API?

The Reflection API is used to examine and manipulate classes, methods, fields, and constructors during runtime, even if their details are not known at compile time.

It is provided by the java.lang and java.lang.reflect packages and is widely used in frameworks, tools, and advanced Java applications.

Where Reflection API is Used?

The Reflection API is mainly used in the following areas:

  • IDE (Integrated Development Environment): The Reflection API us used to analyze code and provide features like auto-completion and code inspection (e.g., Eclipse, NetBeans).
  • Debugger: The Reflection API helps in examining and modifying program behavior during runtime for debugging purposes.
  • Test Tools: The Reflection API is used in testing frameworks to dynamically inspect and execute code.

java.lang.Class class

The java.lang.Class class is used to represent a class at runtime.

It mainly performs the following tasks:

  • Provides methods to get the metadata of a class at runtime.
  • Provides methods to examine and modify the runtime behavior of a class.

Commonly Used Methods of Class Class

The Class class provides several methods to retrieve metadata and work with classes at runtime.

MethodDescription
1) public String getName()returns the class name
2) public static Class forName(String className)throws ClassNotFoundExceptionloads the class and returns the reference of Class class.
3) public Object newInstance()throws InstantiationException,IllegalAccessExceptioncreates new instance.
4) public boolean isInterface()checks if it is interface.
5) public boolean isArray()checks if it is array.
6) public boolean isPrimitive()checks if it is primitive.
7) public Class getSuperclass()returns the superclass class reference.
8) public Field[] getDeclaredFields()throws SecurityExceptionreturns the total number of fields of this class.
9) public Method[] getDeclaredMethods()throws SecurityExceptionreturns the total number of methods of this class.
10) public Constructor[] getDeclaredConstructors()throws SecurityExceptionreturns the total number of constructors of this class.
11) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityExceptionreturns the method class instance.

Getting Class Object in Java

There are three ways to get the instance of the Class class:

  • forName() method of Class class
  • getClass() method of Object class
  • .class syntax

1. Using forName() Method

It is used to load the class dynamically and returns the instance of the Class class. It should be used when you know the fully qualified name of the class. It cannot be used for primitive types.

In the following example, we use the forName() method to get the Class object.

Output:

2. Using getClass() Method

The getClass() method returns the instance of the Class class from an object. It is used when the type is known.

In the following example, we use the getClass() method.

Output:

3. Using .class Syntax

If a type is available but no instance exists, we can obtain the Class object using .class. It can also be used with primitive data types.

In the following example, we use .class syntax.

Output:

Determining the class object

The following methods of Class class are used to determine the class object:

1) public boolean isInterface(): determines if the specified Class object represents an interface type.

2) public boolean isArray(): determines if this Class object represents an array class.

3) public boolean isPrimitive(): determines if the specified Class object represents a primitive type.

Let's see the simple example of reflection API to determine the object type.

Output:

Advantages and Disadvantages of Using Reflection

Reflection is a powerful feature in Java, but it should be used carefully. Let's understand its advantages and disadvantages in simple terms.

Advantages

  • Reflection allows you to inspect classes, methods, and fields at runtime.
  • You can create objects and call methods dynamically using Reflection.
  • Reflection is useful in frameworks, IDEs, and development tools.
  • Reflection helps in building flexible and reusable code.

Disadvantages

  • Reflection breaks encapsulation by accessing private members.
  • Reflection can expose sensitive data, leading to security risks.
  • Reflection slower performance due to runtime processing.
  • Reflection makes code harder to understand and maintain.

Conclusion

Reflection is an advanced feature in Java that provides great flexibility, but it should be used only when necessary. It may affect performance and security, so it is best suited for frameworks and tools rather than regular application code.