Java Reflection class get fields declared

Introduction

The following methods are available to retrieve fields of a class:

public Field getDeclaredField(String name)  
public Field[] getDeclaredFields()  

public Field getField(String name)  
public Field[] getFields()  
import java.lang.reflect.Field;

public class Main {
   public static void main(String args[]) throws Exception {
      Class c = Class.forName("java.lang.String");
      Field[] fields = c.getDeclaredFields();
      System.out.println("No of fields : " + fields.length);
      for (Field f : fields)
         System.out.println(f);//from   w w w .  j  a va 2  s  .  c  om
   }
}



PreviousNext

Related