How to Find All the Constructors, Fields and Methods of a Class in JAVA

Introduction

 
The process of finding Constructors, Fields, and Methods in a class In Java programming language is done by the Reflection API in Java. Reflection is commonly used by programs which require the ability to examine or modify the runtime behaviour of applications running in the Java virtual machine.
 

What is Reflection API in Java? 

 
It is possible to use Reflection in Java; Java provides the API in a package named "reflect". Reflection is a feature in the Java programming language. Reflection allows an executing Java program to "introspect" itself, and manipulate internal properties of the program. For example, it is possible for a Java class that has n number of constructors, methods, and fields to be accessed; the details of all the functions (methods) with help of appropriate Class of the reflect package; for example you want to find the constructor of a class then you can use the Constructor class.
 

Constructor class

 
Using the Constructor class of the Java reflection API, you can inspect the Constructors of classes and instantiate objects at runtime.
 
Example
  1. import java.lang.reflect.Constructor;  
  2. public class FindConstructor 
  3. {  
  4.     public static void main(String args[]) 
  5.     {  
  6.         try {  
  7.             /* Make the Object of Class class and give the fully fath of desired class for finding the constructor  in this example we give the                               java.io.BufferedInputStream class */  
  8.             Class c = Class.forName("java.io.BufferedInputStream");  
  9.             System.out.println("Constructors:");  
  10.             /*by using getConstructor method for get the constructor for Class obj(c)*/  
  11.             Constructor constructors[] = c.getConstructors();  
  12.             for (int i = 0; i < constructors.length; i++)  
  13.                 System.out.println(+i + "-" + constructors[i]);  
  14.         } catch (Exception e) {  
  15.             System.out.println("Exception: " + e);  
  16.         }  
  17.     }  
  18. }  
0utput
 
Findconstructor.gif
 

Fields Class

 
Using the Field class of the Java reflection API you can inspect the Fields of classes and obtain the object's field name, type and the getting and setting fields value.
 
Example
  1. import java.lang.reflect.Field;    
  2. public class MyField   
  3. {  
  4.     public static void main(String args[])   
  5.     {  
  6.         try {    
  7.             /* Make the Object of Class class and give the fully path of desired class for finding the constructor in this example we give the java.lang.reflect.Field class as parameter of forName method */  
  8.             Class c = Class.forName("java.lang.reflect.Field");  
  9.             System.out.println("Fields:");  
  10.             Field fields[] = c.getFields();  
  11.             for (int i = 0; i < fields.length; i++)  
  12.                 System.out.println(+i + "- " + fields[i]);  
  13.         } catch (Exception e) {  
  14.             System.out.println("Exception: " + e);  
  15.         }  
  16.     }  
  17. }  
Output
 
MyField.gif
 

Method class

 
A Method class providing information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).
  
Example
  1. import java.lang.reflect.Method;  
  2. public class MyMethod  
  3. {  
  4.     public static void main(String args[])   
  5.     {  
  6.          try   
  7.          {  
  8.   
  9.             /* Make the Object of Class class and give the fully path of desired class for finding the constructor  in this example we give the                   java.lang.reflect.Method class as parameter of forName method */  
  10.             Class c = Class.forName("java.lang.reflect.Method");  
  11.             System.out.println("methods:");  
  12.             Method methods[] = c.getMethods();  
  13.             for (int i = 0; i < methods.length; i++)   
  14.             System.out.println(+i+"- " + methods[i]);  
  15.          }   
  16.          catch (Exception e)   
  17.          {  
  18.               System.out.println("Exception: " + e);  
  19.          }  
  20.     }  
  21. }  
Output 
 
 Methodclass image.gif
 

Summary

 
With the help of this article, you can access any field, constructor or method of any class and you can get the internal structure of an unknown class so Reflection has its importance.


Similar Articles