coderz.py

Keep Coding Keep Cheering!

Reflection in Java

java thread class

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.

  • Reflection informs us about the class to which an object belongs, as well as the methods of that class that can be called using the object.
  • We can invoke methods at runtime using reflection, regardless of the access specifier used with them.
  • One advantage of the reflection API is that we can also manipulate private members of the class.
Reflection API In Java

Using Reflection API, we can implement the reflection on the following entities:

  • Field: The Field class contains information used to declare a variable or a field, such as a data type (int, double, String, and so on), access modifier (private, public, protected, and so on), name (identifier), and value.
  • Method: The Method class can assist us in extracting information such as the method’s access modifier, method return type, method name, method parameter types, and exception types raised by the method.
  • Constructor: Constructor class gives information about class constructor that includes constructor access modifier, constructor name, and parameter types.
  • Modifier: The modifier class gives us information about a specific access modifier.
java.lang.Class class:

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:

  • Methods for retrieving class metadata during runtime.
  • Methods for inspecting and modifying a class’s behavior at run time.
Class class methods that are frequently used:
SrMethodDescription
1. public String getName()returns the class name
2.public static Class forName(String className)throws ClassNotFoundExceptionLoads the class and returns the Class class reference.
3. public Object newInstance()throws InstantiationException,IllegalAccessExceptioncreates 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 SecurityExceptionreturns the total number of fields of this class.
9. public Method[] getDeclaredMethods()throws SecurityExceptionThis method returns the total number of methods in this class.
10. public Constructor[] getDeclaredConstructors()throws SecurityExceptionThis method returns the total number of Constructors in this class.
11.public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityExceptionreturns the method class instance.
Create java.lang.Class Objects:

We can create java.lang.Class objects using one of the following methods.

  • forName() method of Class class
  • getClass() method of Object class
  • the .class syntax
1) forName() method

It is used to dynamically load the class and returns a Class class instance

class obj_test = Class.forName (“Test”);
Example:
class Coderz{}    
    
public class Test{    
 public static void main(String args[]) throws Exception {    
  Class c=Class.forName("Coderz");    
  System.out.println(c.getName());    
 }    
}    
Output:
Coderz
2) getClass () method

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 ();
Example:
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);  
 }  
}  
Output:
Coderz
3) .class extension

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;
Example:
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

Note: also read about the Character class in Java

Follow Me

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

Leave a Comment

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

Advertisement