Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Nov 29, 2001

Print Out the List of Fields and Methods Declared in Any Class


Sometimes, it is essential to know the fields and methods that are declared in a particular class. This is all the more important when there is no access to the source code of the class. This code fragment can be used to print all the fields and methods in any given class.
 
import java.lang.reflect.*;
public class ClassMetaData {

 public static void main(String[] args) {
   try {
      Class c = Class.forName("ClassMetaData");
      Field[] fields = c.getDeclaredFields();
      System.out.println("List of declared fields _
are . . .");
      for (int i = 0 ; i < fields.length; i++)
          System.out.println(fields[i]);

      Method[] methods = c.getDeclaredMethods();
      System.out.println("List of declared methods _
are . . .");
      for (int i = 0; i < methods.length; i++)
          System.out.println(methods[i]);
   }
   catch(ClassNotFoundException excep) {
      System.err.println("Unable to find class");
   }
 }
}
Shashank Tanksali
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date