It is the process of examining / modifying the runtime behaviour of an object at runtime.
The Reflection API is mainly used in:
IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
Debugger
Test Tools etc.
No. It's like challenging the design of application.
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.
Because it has to inspect the metadata in the bytecode instead of just using pre compiled addresses and constants.
Java Reflection is a process of examining or modifying the run time behavior of a class at run time.
The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.
The java.lang and java.lang.reflect packages provide classes for java reflection.
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. |
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
Following methods of Class class is used to determine the class object:
public boolean isInterface(): determines if the specified Class object represents an interface type.
public boolean isArray(): determines if this Class object represents an array class.
public boolean isPrimitive(): determines if the specified Class object represents a primitive type.
Let's see the simple example of reflection api to determine the object type.
class Simple{}
interface My{}
class Test{
public static void main(String args[]){
try{
Class c=Class.forName("Simple_JBK");
System.out.println(c.isInterface());
Class c2=Class.forName("My");
System.out.println(c2.isInterface());
}
catch(Exception e){
System.out.println(e);
}
}
}
Output:
false true
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_JBK");
System.out.println(c.getName());
}
}
Output:
Simple_JBK