May 7, 2026

Advanced Java – What is Reflection?

In this tutorial, you will learn about Reflection in Java and how to use it.

  1. Overview of Java Reflection
  2. How Java Reflection Works
  3. Benefits of Java Reflection

 

1. Overview of Reflection in Java

Reflection means a program looking at itself and accessing it’s properties. It is an advance Java programming feature that allows the program to manipulate it’s internal properties at run time.

For example, a class in Java can use reflection to obtain all the member variables and methods. This information can be obtained even if those variables and methods are private or final.

As you know, you cannot access private methods from outside a class. However, with reflection you can now access those private methods.

 

2. How Java Reflection Works

Below are example how refection use-cases:

Retrieving the fields of a class

In the code below we provide a class with private fields and methods.

public class Car {

	private String model;
	
	private int available = 8;
	
	public Car(String model, int available) {
		this.model = model;
		this.available = available;
	}
	
	private int getAvailable() {
		return available;
	}
	
	private void start() {
		System.out.println("Engine started and runing...");
	}
	
	private void move() {
		System.out.println("Gear engages, accelerating...");
	}
}

 

In the main method below, we instantiate our car class and then use reflection to retrieve and print out all the fields of the class.

public static void main(String[] args) {
			
	Car myCar = new Car("Toyota Camry", 9);
	
	print(car.name); // Error: name is not visible
	
	//Now using reflection
	Field[] carFields = myCar.getClass().getDeclaredFields();
	
	for(Field field: carFields) {
		System.out.println(field.getName());
	}
}

If you execute the main() method above then

  • model
  • available

would be printed to the console

 

Setting the private field of a class

In the main method below, we set the values of the model using reflection.

for(Field field: carFields) {
	if(field.getName().equals("model")) {
		field.setAccessible(true);
		field.set(myCar, "Range Rover");
	}
}			
System.out.println(myCar.getModel());

This code actually changes the value of the private field model from “Toyota Camry” to “Range Rover”.

Note: I added a public getModel() method to the car class to be able to print this value to the output.

 

Calling a private method

In the code below we use reflection to call a private method.

Method[] carMethods = myCar.getClass().getDeclaredMethods();
for(Method method: carMethods) {
	method.setAccessible(true);
	if(method.getName().equals("start")) {
		method.invoke(myCar);
	}
}	

Note that the invoke() method takes the instance of the object as a parameter.

 

Calling a static method

This is similar to call a non-static method with the difference that the invoke() would take null as parameter if the method being invoked is static.

 

Calling a method with parameters

In this case, you will pass the arguments to the invoke() method following the first argument which will always be the name of the instance. Of course, all the parameters are comma-separated.

 

3. Benefits/Uses of Reflection

Here are some of the benefits of reflection.

  • Testing – In testing scenarios where the behaviour of a method depends on it’s private fields, reflection can be used to gain access to those private fields
  • Debugging – Can be used by debugging tools
  • Extensibility – An application (a plugin or framework for instance) and use user defined classes by using their fully-qualified-names to create extensibility just from object instances.

 

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x