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

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");   } }}
See also  Redefining Energy Management: Advanced Solutions for Modern Buildings

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist