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.
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.
The Reflection API is mainly used in the following areas:
The java.lang.Class class is used to represent a class at runtime.
It mainly performs the following tasks:
The Class class provides several methods to retrieve metadata and work with classes at runtime.
| Method | Description |
|---|---|
| 1) public String getName() | returns the class name |
| 2) public static Class forName(String className)throws ClassNotFoundException | loads the class and returns the reference of Class class. |
| 3) public Object newInstance()throws InstantiationException,IllegalAccessException | creates 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 SecurityException | returns the total number of fields of this class. |
| 9) public Method[] getDeclaredMethods()throws SecurityException | returns the total number of methods of this class. |
| 10) public Constructor[] getDeclaredConstructors()throws SecurityException | returns the total number of constructors of this class. |
| 11) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException | returns the method class instance. |
There are three ways to get the instance of the Class class:
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:
Simple
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:
Simple
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:
boolean
Test
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:
false true
Reflection is a powerful feature in Java, but it should be used carefully. Let's understand its advantages and disadvantages in simple terms.
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.
We request you to subscribe our newsletter for upcoming updates.