Java Reflection is a process of examining or modifying the run time behavior of a class at run time. Java Reflection API is used to manipulate class and its members which include fields, methods, constructor, etc. at runtime. In This article we would understand Java Reflection API in detail.
This article will focus on following pointers:
- Where Java Reflection API used?
- Class in java.lang.reflect Package
- Methods used in java.lang.Class
- How to get the object of Class class?
- Advantages and Disadvantages of Using Java Reflection API
So let us get started with these pointers in this article on Java Reflection API
Where is Java Reflection API used?
The Reflection API is mainly used in:
- IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
- Debugger
- Test Tools etc.
So what is Class in Java lang reflect package?
Class in java.lang.reflect Package?
Following is a list of various Java classes in java.lang.package to implement reflection-
- Field: This class is used to gather declarative information such as datatype, access modifier, name and value of a variable.
- Method: This class is used to gather declarative information such as access modifier, return type, name, parameter types and exception type of a method.
- Constructor: This class is used to gather declarative information such as access modifier, name and parameter types of a constructor.
- Modifier: This class is used to gather information about a particular access modifier.
No let us take a look at Java Reflection API methods,
Methods used in java.lang.Class
Method | Description |
public String getName() | returns the class name |
public static Class forName(String className)throws ClassNotFoundException | loads the class and returns the reference of Class class. |
public Object newInstance()throws InstantiationException,IllegalAccessException | creates new instance. |
public boolean isInterface() | checks if it is interface. |
public boolean isArray() | checks if it is array. |
public boolean isPrimitive() | checks if it is primitive. |
public Class getSuperclass() | returns the superclass class reference. |
public Field[] getDeclaredFields()throws SecurityException | returns the total number of fields of this class. |
public Method[] getDeclaredMethods()throws SecurityException | returns the total number of methods of this class. |
public Constructor[] getDeclaredConstructors()throws SecurityException | returns the total number of constructors of this class. |
public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException | returns the method class instance. |
Let us move forward with article,
How to get the object of Class class?
There are 3 ways to get the instance of Class class. They are as follows:
- forName() method of Class class
- getClass() method of Object class
- the .class syntax
forName() method of Class class
- is used to load the class dynamically.
- returns the instance of Class class.
- It should be used if you know the fully qualified name of class.This cannot be used for primitive types.
Let’s see the simple example of forName() method.
class Simple{} class Test{ public static void main(String args[]){ Class c=Class.forName("Simple"); System.out.println(c.getName()); } }
Output:
Simple
Java Reflection: API getClass() method of Object class
It returns the instance of Class class. It should be used if you know the type. Moreover, it can be used with primitives.
class Simple{} class Test{ void printName(Object obj){ Class c=obj.getClass(); System.out.println(c.getName()); } public static void main(String args[]){ Simple s=new Simple(); Test t=new Test(); t.printName(s); } }
Output:
Simple
The .class syntax
If a type is available but there is no instance then it is possible to obtain a Class by appending “.class” to the name of the type.It can be used for primitive data type also.
class Test{ public static void main(String args[]){ Class c = boolean.class; System.out.println(c.getName()); Class c2 = Test.class; System.out.println(c2.getName()); } }
Output:
boolean
Test
Now let us continue with this Java Reflection API article
Advantages and Disadvantages of Using Java Reflection API
Advantages of using Java Reflection API
- Extensibility Features: An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
- Debugging and testing tools: Debuggers use the property of reflection to examine private members in classes.
Disadvantages of using Java Reflection API
- Performance Overhead: 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.
- Exposure of Internals: Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
Thus we have come to an end of this article on ‘Java Reflection API’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.