Reflection in Java

In this post, we will see what is a reflection in java and what are the benefits. Also, we will see how to use reflection in real time scenario.

Reflection in Java- Reflection is a way to change the behavior of a class at runtime using different predefined API.

Benefits of reflection –

  • We can get information about class members(fields, constructors and methods) using reflection API(see examples).
  • We can access private members using reflection(see examples).
  • We can define our framework/custom utility methods using reflection, which can be used later in our development.
  • Sometimes reflection also useful to write JUnit for private methods(see examples).
  • To IDE development like an eclipse, IntelliJ, STS.

Disadvantage –

  • While using reflection it may result as a performance issue.
  • We can access private fields outside of class. We are breaking encapsulation.

Let’s see some predefined API which frequently used in Reflection mechanism.

  • Class – This is a final class available in java.lang package.
  • Method – This is a final class which is available in java.lang.reflect package.
  • Field – This is also a class final which is available in java.lang.reflect package.
  • Constructor – This is also a final class available in java.lang.reflect package.
  • Annotation – This is an interface available in java.lang.annotation package.

 

Class class in java – 

Let’s see some example related to Class.

Example 1 – Get the Class class object and calling their methods.

 

Output is –

Super class of class name class java.lang.Object
Fully qualified class name is refection1.ClassExample1
class name without package ClassExample1
Class loader name is [email protected]
package name is package refection1

Example 2 – In this example, we will get constructors details.

Output is –

First Constructor is — public refection1.Example2()
Second Constructor is — public refection1.Example2(java.lang.String)

 

Note – The method getConstructors() only return list of constructor which is public. If you use any other modifier than public, it will return an empty array. Let’s see an example.

Example 3 –

Output is – 0

If we will try to access the element from constructorArray we will get java.lang.ArrayIndexOutOfBoundsException.

Note – We can access even other constructors( including private), using method setAccesible(true), We will see later.

 

Example 4 – In this example, we will get all the methods and fields using getDeclaredMethods() and getDeclaredFields() methods.

Output is –

public static void refection1.Example4.main(java.lang.String[]) throws java.lang.ClassNotFoundException
public void refection1.Example4.m1()
public void refection1.Example4.m2()
private void refection1.Example4.m3()
—–fields inside class Example4—–
private java.lang.String refection1.Example4.name
private int refection1.Example4.a

 

Method class example

Example 1 –

output is –

—–get parameter count—-
1
0
—–get the method name—-
main
m1
–get the method return type–
void
void

Field class example – 

Example –

Output is –

—–get filed name—-
name
a
—–get field type name—-
class java.lang.String
int

 

Constructor class example – 

Output is –

—–get constructor name—-
public refection1.Example6()

So far we have seen the basics of some classes and methods used in reflection. Let’s see how to access private members(field, methods, and constructors) outside of any class. Suppose we have some java beans class where we have defined private variables. Generally, we can access these private variables only using getter and setter. Same way if have private methods and constructors we cannot access outside of any class. Let’s see a couple of examples where we will access private members outside of that class.

Accessing private variable outside of class – We have two classes Employee and Example1 in same package reflection1. In Employee class we have a private field private String name = “ram”.

Employee.java

Example1.java

Let’s run the Example1.java.

Output is –

Exception in thread “main” java.lang.IllegalAccessException: Class reflection1.Example1 can not access a member of class reflection1.Employee with modifiers “private”
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.checkAccess(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at reflection1.Example1.main(Example1.java:16)

We are not able to access private variable outside the class. Let’s use method setAccessible(true).

Output is –

name is — ram

Accessing private method outside of class –

Employee.java

Example1.java

Output is –

name is — mohan

 

Accessing private Constructor outside of class –

Employee.java

Example1.java

Output is –

i am a private constructor

 

 

 

 

 

 

 

 

 

Top