Reflection In Java

This section describes you about the Reflection in Java.

Reflection In Java

This section describes you about the Reflection in Java.

Reflection In Java

Reflection In Java

In this section we will read about the various aspects of Reflection in Java such as, what is reflection, use of reflection, drawback of reflection etc.

What Is Reflection

Reflection in computer science is referred to as a computer program that analyzes and/or modify the object's structure and behavior at run time. Object's structure and behavior may be the values, meta-data, properties and functions, etc. Most of the high level virtual machine programming languages, manifestly typed or statically typed programming languages for example, Smalltalk, Java, C#, Scala, scripting languages, uses reflection.

Use Of Reflection

As per the above section, use of reflection is to observe and/or modify the program execution at run time. In an object oriented programming, the reflection may be used in to inspect the the classes, interfaces, fields, and methods, at runtime. For this there is no need to know their names at compile time. Apart, from these uses reflection is used to instantiate the new object and calling the methods. Reflection may be used in the following areas :

  • Used in IDE (Integrated Development Environment)
  • Used in Debugger.
  • Used in Test tools

Drawbacks of Reflection

Reflection is useful and powerful but, it has also some drawbacks. If in the programming code there is no need to use the reflection it shouldn't be used. If you use the reflection in your code following issues you may have to face :

  • Performance : If you use the reflection in your code your application performance may degrade.
  • Security : As Reflection require a runtime permission it may not run under security manager so the reflection should not be used in a restricted security context.
  • Internal exposure : Reflection allows code that can access private fields and methods and hence should not be used as it may render code dysfunctional and destroys portability.

Example

Here I am giving a simple example which will demonstrate you about how to use the reflection in Java. In this example I have created a Java class named ReflectionExample.java inside I have defined a static constant field. Then I have created another class named MainClass.java where I have created a main method and find the class using getClass() method then I have used the method getField() of java.lang.Class class then I have used the get() method of Field class to get the value of the field.

ReflectionExample.java

package net;

public class ReflectionExample {
	
	public static int a = 10;	
}

MainClass.java

package net.roseindia;

import java.lang.reflect.Field;

import net.ReflectionExample;
public class MainClass {
	
	public static void main(String args[])
	{
		
			ReflectionExample re = new ReflectionExample();			
			Class c1 = re.getClass();
			Field field;
			try {
				field = c1.getField("a");
				System.out.println("Field of Class Reflection Example : "+field);				
				Object value = field.get(re);
				System.out.println("Value of the field : "+value);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
	}

}

Output :

When you will compile and execute the MainClass.java then the output will be as follows :

Download Source Code

For reading more in detail about Reflection you may go through the link http://www.roseindia.net/java/reflect/reflectionOverview.shtml