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.
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.
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.
The forName() method belongs to Class and is used by providing the name of the class you want to perform your reflection.
The getClass method is called on the object of a class to retrieve and reflect the class you want.
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.
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.
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.
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.
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.
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.