Obtain a list of all public fields, both declared and inherited in Java

Description

The following code shows how to obtain a list of all public fields, both declared and inherited.

Example


/*from   ww  w .  j a va2 s. c o m*/
import java.lang.reflect.Field;

public class Main {
  public static void main(String[] argv) throws Exception {
    Class cls = java.awt.Point.class;
    
    Field[] fields = cls.getFields();
    for (int i = 0; i < fields.length; i++) {
      Class type = fields[i].getType();
      System.out.println(fields[i]);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy