Java Reflection Tutorial

Introduction to Java Reflection:

We gained knowledge on Java collections framework from our previous tutorial. Here, we will see about Java Reflection.

Reflection is to inspect and change the behavior of a program at runtime.

With the help of this reflection API, you can inspect classes, constructors, modifiers, fields, methods, and interfaces at runtime. For Example, you can get the name of the class or you can get details of the private members of the class.

Read through our entire JAVA training series for more insight on Java concepts.

Here is a Video tutorial on Java Reflection:

Where is Refection Used

  • Reflection is mainly used in the debugging tools, junit and frameworks to inspect and change the behavior at runtime.
  • It is used, when your application has third-party libraries and when you want to know about the classes and methods available.

Reflection API

java.lang.reflect package has the classes and interfaces to do reflection. And the java.lang.class can be used as an entry point for the reflection.

How to get the class objects:

1. If you have instance of an object,

class c=obj.getclass();

2. If you know the type of the class,

class c =type.getClass();

3. If you know the class name,

Class c = Class.forName(“com.demo.Mydemoclass”);

How to get the class members:

Class members are fields (class variables) and methods.

  • getFields() – Used to get all the fields except the private fields.
  • getDeclaredField() – Used to get the private fields.
  • getDeclaredFields() – Used to get the private and public fields.
  • getMethods()– Used to get all the methods except the private methods.
  • getDeclaredMethods() –Used to get the public and private methods.

Demo Programs:

ReflectionHelper.java:

This is the class where we are going to inspect using the reflection API.

 

class ReflectionHelper {

private int age;
private String name;
public String deptName;
public int empID;

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
} 

ReflectionHelper

ReflectionDemo.java

 
public class ReflectionDemo {

public static void main(String[] args) throws
NoSuchFieldException, SecurityException {

//get the class
Class
ReflectionHelperclass=ReflectionHelper.class;

//get the name of the class

String className = ReflectionHelperclass.getName();
System.out.println("className=="+className);

System.out.println("getModifiers"+ReflectionHelperclass.getModifier
s());

System.out.println("getSuperclass"+ReflectionHelperclass.getSupercla
ss());

System.out.println("getPackage"+ReflectionHelperclass.getPackage());

Field[] fields =ReflectionHelperclass.getFields();
//getting only the public fields for(Field oneField : fields) {

Field field = ReflectionHelperclass.getField(oneField.getName());

String fieldname = field.getName();

System.out.println("only the public fieldnames:::::"+fieldname);

}
//getting all the fields of the class
Field[] privatefields
=ReflectionHelperclass.getDeclaredFields();
for(Field onefield : privatefields) {
Field field =
ReflectionHelperclass.getDeclaredField(onefield.getName());
String fieldname = field.getName();

System.out.println("all the fieldnames in the class:::"+fieldname);

}

Method[] methods
=ReflectionHelperclass.getDeclaredMethods();
for(Method m: methods) {

System.out.println("methods::::"+m.getName());
}

}} 

ReflectionDemo1ReflectionDemo2

Drawbacks of Reflection

  • As reflection involves runtime dynamic operations, it may lead to performance issue.
  • Reflection allows to expose the internals (like accessing private members), and this may lead to breaking the abstraction.

Points to Remember

  • Reflection is inspecting about the classes and class members at runtime and changing the behavior.
  • java.lang.reflect package has the classes and interfaces to inspect the class members or interfaces at runtime.

Conclusion

In this tutorial, we learned about reflection and how to modify and inspect the program at runtime.

Watch out our upcoming tutorial to know more about Swing!!

PREV Tutorial | NEXT Tutorial