Usage of Reflection API in Java with example

Usage of Reflection API in Java

Most of us might not know the usage of Reflection API in Java. It is because mostly we don’t use it directly in our code. But it is used internally by many frameworks / components. In this post we will discuss some of the usage of reflection.

What is reflection?

Reflection in Java is the ability to examine and/or modify the properties or behavior of an object at run-time. Reflection makes it possible to inspect classes, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.

The reflection API mechanism is built into the class named Class. This class provides methods to get the metadata of a class at run time. Some of the useful methods in Class are described below,

  • forName – load a class of a given name, using the current class loader and return the Class object associated with the class
  • getName – Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
  • newInstance – Creates a new instance of the class represented by this Class object.
  • getFields – Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
  • getMethods – Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Why do we need reflection?

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

  • Examine an object’s class at runtime
  • Construct an object for a class at runtime
  • Examine a class’s field and method at runtime
  • Invoke any method of an object at runtime
  • Change accessibility flag of Method and Field.
  • Map objects to tables in a database at runtime


It is also been used by Debuggers and Test Tools. For instance, Junit uses reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test. Reflection is also used by Apache commons BeanUtils component.

Reflection example

Spring uses reflection to construct a bean. For example, in spring bean configuration file you will define a bean as,

When the Spring context processes this < bean > element, it will use Class.forName(“somepackage.Foo”) to instantiate the Class. It will then again use reflection to get the appropriate setter for the property “someField” and set its value to the specified value.

Below is a simple program showing usage of few methods discussed above.

Hope you find this post useful. The above program shows only a simple usage of reflection API. Refer this tutorial to understand more.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.