Reflection API : in Java

Reflection API : in Java

What is an API?

Application program interface (API) is a set of routines, protocols, and tools for building software applications. An API specifies how software components should interact. Additionally, APIs are used when programming graphical user interface (GUI) components. A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together,and build his own application.

An example, You are buying an item in online through your credit card. You will provide credit card details and press continue button. It will tell you whether your information is correct or not. To provide these results, there are lot of things in the background.
The application will send your credit card details to a remote application which will validate your information and send the result back your application. API is used in this scenario.
Java application programming interface (API) is a list of all classes that are part of the Java development kit (JDK). It includes all Java packages, classes, and interfaces, along with their methods, fields, and constructors. These pre-written classes provide a tremendous amount of functionality to a programmer.

Reflection API:

As we now know what is API in java,So in simple terms we can say Reflection API is a collection of classes which are meant for Reflection operations in java.

What is Reflection?

In Java, the process of analyzing and modifying all the capabilities of a class at runtime is called Reflection.Reflection is a language’s ability to inspect and dynamically call classes, methods, attributes, etc. at runtime.

Reflection is important since it lets you write programs that do not have to know everything at compile time, making them more dynamic, since they can be tied together at runtime.

For example, using reflection at the runtime you can determine what method, field, constructor or modifiers a class supports.

Where it is used?

The Reflection API is mainly used in:
  • IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
  • Debugger
  • Test Tools etc.
For example, say you have an object of an unknown type in Java, and you would like to call a ‘doSomething’ method on it if one exists. Java’s static typing system isn’t really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called ‘doSomething’ and then call it if you want to.
Using Java Reflection Classes you can obtain  information about:
  • Class Name
  • Class Modifies (public, private, synchronized etc.)
  • Package Info
  • Superclass
  • Implemented Interfaces
  • Constructors
  • Methods
  • Fields
  • Annotations
and much more information about java classes.


java.lang.reflect package encapsulates several important interfaces and classes. These classes and interface define methods which are used for reflection.

java.lang.Class class:

The java.lang.Class class performs mainly two tasks:
  • provides methods to get the metadata of a class at run time.
  • provides methods to examine and change the run time behavior of a class.

Commonly used methods of Class class:

Method Description
1) public String getName() returns the class name
2)public static Class forName(String className)throws ClassNotFoundException  loads the class and returns the reference of Class class.
3)public Object newInstance()throws InstantiationException,IllegalAccessException creates new instance.
4) public boolean isInterface() checks if it is interface.
5) public boolean isArray() checks if it is array.
6) public boolean isPrimitive() checks if it is primitive.
7) public Class getSuperclass() returns the superclass class reference.
The following example shows the usage of java.lang.Class.getName() method.

import java.lang.*;

public class SCPTL {

public static void main(String[] args) {

// returns the Class object associated with this class
SCPTL cl = new SCPTL();
Class c1Class = cl.getClass();

// returns the name of the class
String name = c1Class.getName();
System.out.println("Class Name = " + name);
getClass() method returns the instance of Class class and  getName() method returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String(in this case name of class).
Above code will produce following output:

Uses of Reflection

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
Extensibility Features
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.

Class Browsers and Visual Development Environments
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.

Debuggers and Test Tools
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.

Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.
Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
Security Restrictions
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.

Want to learn more about java?