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:

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) { = name;
public String getDeptName() {
return deptName;
public void setDeptName(String deptName) {
this.deptName = deptName;


public class ReflectionDemo {

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

//get the class

//get the name of the class

String className = ReflectionHelperclass.getName();




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
for(Field onefield : privatefields) {
Field field =
String fieldname = field.getName();

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


Method[] methods
for(Method m: methods) {




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.


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