Java Reflection API

by | Oct 29, 2020 | Java

Home » Java » Java Reflection API

Introduction

JAVA Reflection is a behavioural Application Programming Interface (API) that allows users to examine and study all of the attributes and traits of classes, interfaces, fields (scope should be within the class concerned), and its methods at runtime. Even constructor behaviour falls under the examination bracket of the JAVA Reflection API.

To be able to implement the JAVA Reflection API, all we need to do is import the following package into our code: import java.lang.reflect.* ;

Another package that has a significant importance when it comes to using this API is the java.lang.Class package. Some methods of this class are used to harness the complete metadata of a particular class as well as change significant behaviour of the same class at run time.

Block Diagram

JAVA Reflection API

Where is it Used?

Before moving on to the benefits and disadvantages of using this API, let us discuss a few instances where the Java Reflection API is used. It finds most use in Database Tables. A database normally has a huge collection of data objects, the information within which can be easily retrieved using Java Reflection. This API also forms an integral part of IDEs (Integrated Development Environments) like Eclipse, MyEclipse, NetBeans, and Intellij. Some debuggers and Test Tools also use this mechanism of reflection into classes during runtime.

Advantages of using Reflection API in Java

The main advantage that users get using the Reflection API is the extensibility features that it brings along with it. It allows users to program separate user-defined classes, the objects, and attributes of which can be extended into the main framework of the program. As mentioned above, debuggers and code testers also use the Reflection API to get a better look at private fields, methods, and functions of a class. It gives a better understanding of what is happening in and outside a particular class and its linkages to other segments of the code.

Disadvantages of using Reflection API in Java

As the Reflection API undergoes a complete process of component evaluation of classes and its components, the performance and speed of delivery take a beating in the process. If you are well versed in certain sections of the code that are required to deliver high levels of efficiency and speed, it is recommended not to use the API there.

Another major disadvantage that might take place is in a client-server environment. The API might as well compromise abstraction levels at times when it tried to modify already built classes and its private components. Clients might be able to access code that generally should not concern them as it has nothing to do with the implementation.

Class class

This refers to the java.lang.Class package. It has inbuilt methods that can be called. The package falls under java.lang.reflection.

Let us write a basic code to obtain the object of ‘Class class’ using reflection api.

class Name{}                      //This Name class is of the type ‘Class’. An object of this class will be created

class PrintName{
public static void main (String args[]) {
Class test = Class.forName(“Hello, my name is Rakesh”);
System.out.prinln(test.getName());
}
}

/*This program uses the forName() method and getName() method. Both these methods are predefined in the Class package. The first one is to create an object of Name Class and the next one is to retrieve data from the object. */

 

Output:

Hello my name is Rakesh

//Below is a program using defined methods some basic functionality of the class object.
//We will create a class and an interface and check whether the class object created is of Interface //type.

class classTest{}
interface interTest{}



class Sample{

public static void main (String args[]) {
try{                                //This bit of code has been placed under the try catch block to handle exceptions
Class obj1 = Class.forName(“classTest”);
System.out.println(obj1.isInterface());

Class obj2 = Class.forName(“interTest”);
System.out.println(obj2.isInterface());
}
catch (Exception e) {System.out.println(e);}
}
}

/*isInterface() is a boolean method that returns true if the object represents an interface type and false otherwise. It is also another pre-defined method*/

 

Output

false
true

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author