dcsimg
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 30, 2001

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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