JAVA CODE TO GET ALL MEMBERS (Variables, Methods) OF A CLASS

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

class Testing{

int rollno;
String name;

public Testins(){}

public Testins(int num){}

public Testins(String str){}

public void displaData(){
}

public void intputData(){
}

}

public class ClassInfo {

  public static void main(String[] RK) {

    try {

      // Creates an object of type Class which contains the information of
      // the class Testing defines above.
      Class cl = Class.forName(“Testing”);

      // getDeclaredFields() returns all the constructors of the class.
      Constructor cnst[] = cl.getConstructors();

      // getFields() returns all the declared fields of the class.
      Field fld[] = cl.getDeclaredFields();

      // getMethods() returns all the declared methods of the class.
      Method mtd[] = cl.getMethods();
      System.out.println(“Name of the Constructors of the String class”);

      for (int i = 0; i < cnst.length; i++) {
        System.out.println(cnst[i].getName());
      }

      System.out.println(“Name of the Declared fields”);

      for (int i = 0; i < fld.length; i++) {
        System.out.println(fld[i].getName());
      }

      System.out.println(“Name of the Methods”);

      for (int i = 0; i < mtd.length; i++) {
        System.out.println(mtd[i].getName());
      }

    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

}

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in J2SE. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s