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");   } }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: