Java Reflection: What Is It & Why Does It Matter?

Athena Ozanich
Athena Ozanich

Updated:

Published:

What is a reflection but a mirror image of something? Typically a reflection is reversed of what it is mirroring. In the case of Java, reflection is more straightforward, it’s a way to reflect the information in a class object.

A young woman studying the complex subject of Java reflection, how it works, and how she can use it to improve her software development.

This post will cover everything you need to know to get started with using Java Reflection. You will learn what it does and how you can use it. You will also learn how to inspect and modify classes using the Java Reflection API. Finally, you will also see some code examples of syntax and format for using the Reflection API.

Let’s get started.

Download Now: An Introduction to Java & JavaScript

Java Reflection API

Java reflection is an API used to inspect and modify Java classes, fields, methods, and constructors at runtime. Java has a class named class that collects and stores all information about classes and objects, this class helps facilitate the ability to use reflection.

To perform reflection, you can use the object of a class which is also called a class instance. To do this, you start by creating an object of a class that you want to interact with or modify. Let’s see what that looks like next. Check out this next video on Java reflection to learn a little more about it.

Java Reflection Classes

With Java reflection, the Class class provides three ways to create an object of Class.

.forName()

The forName() method belongs to Class and is used by providing the name of the class you want to perform your reflection.

class Car{...} // create object of Class // to reflect the Car class Class a = Class.forName("Car");

.getClass()

The getClass method is called on the object of a class to retrieve and reflect the class you want.

// create an object of Car class Car c1 = new Car(); // create an object of Class // to reflect Car Class b = c1.getClass();

.class Extension

The .class extension is also used to retrieve and reflect class information. This extension is chained to the end of a class, creating an object of the Class.

// create an object of Class // to reflect the Car class Class c = Car.class;

Using any of the above approaches, you can gain access to the class information at runtime. Let’s look at an example of that next.

Java Reflection: Fields, Methods, and Constructors

Java has a package called java.lang.reflect, which provides multiple classes you can use to modify and manipulate class members such as methods, fields, and constructors.

Java Reflection Field Class

The Java reflection field class is used to perform modifications on a class's fields facilitated through the methods of the Field class.

// access and set the type field Field field1 = obj.getField("type");

Java Reflection Method Class

The Java reflection method class is used to perform modifications on a class's fields facilitated through the methods of the Method class.

// using object of Class to // get all the declared methods of Car Method[] methods = obj.getDeclaredMethods();

Java Reflection Constructors Class

The Java reflection constructor class is used to perform modifications on a class's fields facilitated through the methods of the constructor class.

// get all constructors of Car Constructor[] constructors = obj.getDeclaredConstructors();

Java Reflection Example

Now that you understand how you can interact with the classes and collect information about their members, let's look at this with a code example below.

// put this in Main.java file class Main { public static void main(String[] args) { try { // create an object of Car Car c1 = new Car(); // create an object of Class // using getClass() Class obj = c1.getClass(); // get name of the class String name = obj.getName(); System.out.println("Name: " + name); // get the access modifier of the class int modifier = obj.getModifiers(); // convert the access modifier to string String mod = Modifier.toString(modifier); System.out.println("Modifier: " + mod); // get the superclass of Car Class superClass = obj.getSuperclass(); System.out.println("Superclass: " + superClass.getName()); } catch (Exception e) { e.printStackTrace(); } } }

Using Java Reflection API in your software development

There is a lot to learn about Java reflection, and this post has provided you all the tools you need to continue your journey. From here, the best way to move forward is to learn more about each of the classes that the reflect package offers.

Diving into the different classes can help you learn how to make the best of your reflection efforts by tapping into all the package has to offer.

java

Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about the differences between and uses of these popular programming languages.