SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Abhishek Dubey Articles | Java January 25, 2012
This is the technique to find the internal structure of any class and used its attribute according his definition.
Reader Level:

It is possible to use Reflection in Java; Java provides the API in a package named "reflect". Reflection is a feature in thye 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

import java.lang.reflect.Constructor;
public class FindConstructor
{
     public static void main(String args[])
     {
          try
          {
              /* 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 */
              Class c = Class.forName("java.io.BufferedInputStream");
              System.out.println("Constructors:");
              /*by using getConstructor method for get the constructor for Class obj(c)*/
              Constructor constructors[] = c.getConstructors();
              for (int i = 0; i < constructors.length; i++)
              System.out.println(+i+ "-" + constructors[i]);
          }
          catch (Exception e)
          {
             System.out.println("Exception: " + e);
          }
     }
}

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

import java.lang.reflect.Field;
public class MyField
{
public static void main(String args[])
  {
   try
   {

/* 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.lang.reflect.Field class as parameter of forName method */
        Class c = Class.forName("java.lang.reflect.Field");
        System.out.println("Fields:");
         Field fields[] = c.getFields();
        for (int i = 0; i < fields.length; i++)
          System.out.println(+i+"- " + fields[i]);
        }
        catch (Exception e)
        {
             System.out.println("Exception: " + e);
        }
    }
}

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

import java.lang.reflect.Method;
public class MyMethod
{
    public static void main(String args[])
    {
         try
         {

            /* 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.lang.reflect.Method class as parameter of forName method */
            Class c = Class.forName("java.lang.reflect.Method");
            System.out.println("methods:");
            Method methods[] = c.getMethods();
            for (int i = 0; i < methods.length; i++)
            System.out.println(+i+"- " + methods[i]);
         }
         catch (Exception e)
         {
              System.out.println("Exception: " + e);
         }
    }
}

Output 

 Methodclass image.gif

Summary

With the help of this example you can access any field, constructor or method of any class and you can get the internal structure of an unknown class so reflection have its importantance.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor