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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…