Reflection is a runtime API for inspecting and modifying the behavior of methods, classes, and interfaces. The classes required for reflection are provided in the java.lang.reflect package, which is required to understand reflection.
Using Reflection API, we can implement the reflection on the following entities:
At runtime, the java.lang.Class class contains all the information and data about classes and objects. This is the primary class for reflection.
This class provides:
Sr | 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 Class class reference. |
3. | public Object newInstance()throws InstantiationException,IllegalAccessException | creates a new instance. |
4. | public boolean isInterface() | determines whether it is an interface |
5. | public boolean isArray() | determines whether it is an array |
6. | public boolean isPrimitive() | determines whether it is primitive. |
7. | public Class getSuperclass() | returns the superclass class reference. |
8. | public Field[] getDeclaredFields()throws SecurityException | returns the total number of fields of this class. |
9. | public Method[] getDeclaredMethods()throws SecurityException | This method returns the total number of methods in this class. |
10. | public Constructor[] getDeclaredConstructors()throws SecurityException | This method returns the total number of Constructors in this class. |
11. | public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException | returns the method class instance. |
We can create java.lang.Class objects using one of the following methods.
It is used to dynamically load the class and returns a Class class instance
class obj_test = Class.forName (“Test”);
class Coderz{}
public class Test{
public static void main(String args[]) throws Exception {
Class c=Class.forName("Coderz");
System.out.println(c.getName());
}
}
Coderz
It returns a Class class instance. If you know the type, you should use it. It can also be used with primitives.
Test obj = new Test ();
Class obj_test = obj.getClass ();
class Coderz{}
class Test{
void printName(Object obj){
Class c=obj.getClass();
System.out.println(c.getName());
}
public static void main(String args[]){
Coderz s=new Coderz();
Test t=new Test();
t.printName(s);
}
}
Coderz
If a type is available but no instance exists, a Class can be obtained by appending “.class” to the type’s name. It can also be used for primitive data types.
Class obj_test = Test.class;
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());
}
}
boolean
Test
Note: also read about the Character class in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…