Java Reflection API provides us with the necessary tools to inspect classes, interfaces, fields and methods at runtime.
Some of the things which we can do with the help of Reflection are mentioned below:
- Fetch all the information of the Class – such as the name, package, type ,super class etc
- Get the list of all members and methods of the Class
- Get the list of constructors.
- Create an instance of the Class without using new keyword
- Invoke the methods of the Class.
- We can even access private members and methods of the Class.
- Getting and Setting Field Values
What is a Class Object?
To start with Reflection, we need a Class object. All types in Java including the primitive types (int, long, float etc.) including arrays have an associated Class object.
Different Ways to get Class Object
Using name of the class
If you know the name of the class at compile time you can obtain a Class object like this:
Class<ReflectionClassExample> clazz = ReflectionClassExample.class;
Using object of the class
If you have the object of the class, you can obtain a Class object like this:
ReflectionClassExample reflectionClassExample = new ReflectionClassExample();
Class<ReflectionClassExample> clazz1 = (Class<ReflectionClassExample>) reflectionClassExample.getClass();
Using Class.forName()
When using the Class.forName() method you must supply the fully qualified class name. The Class.forName() method may throw a ClassNotFoundException if the class cannot be found on the classpath at runtime.
Class<ReflectionClassExample> clazz2 = (Class<ReflectionClassExample>) Class.forName("com.java.reflection.ReflectionClassExample");
package com.java.reflection;
public class ReflectionClassExample {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ReflectionClassExample reflectionClassExample = new ReflectionClassExample();
Class < ReflectionClassExample > clazz1 = (Class < ReflectionClassExample > ) reflectionClassExample.getClass();
System.out.println("Class Name1:" + clazz1.getName());
try {
Class < ReflectionClassExample > clazz2 = (Class < ReflectionClassExample > ) Class.forName("com.java.reflection.ReflectionClassExample");
System.out.println("Class Name2:" + clazz2.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Class < ReflectionClassExample > clazz = ReflectionClassExample.class;
System.out.println("Class Name:" + clazz.getName());
System.out.println("Class Simple Name:" + clazz.getSimpleName());
System.out.println("Class Package Info:" + clazz.getPackage());
System.out.println("SuperClass:" + clazz.getSuperclass());
System.out.println("Type Name:" + clazz.getTypeName());
}
}
Class Name1:com.java.reflection.ReflectionClassExample
Class Name2:com.java.reflection.ReflectionClassExample
Class Name:com.java.reflection.ReflectionClassExample
Class Simple Name:ReflectionClassExample
Class Package Info:package com.java.reflection
SuperClass:class java.lang.Object
Type Name:com.java.reflection.ReflectionClassExample
Please go through the following relevant articles