Java Reflection - Java Class.getFields()








Syntax

Class.getFields() has the following syntax.

public Field [] getFields()   throws SecurityException

Example

In the following code shows how to use Class.getFields() method.

//  w  w  w .j  av  a2s.  c  o m

import java.lang.reflect.Field;

public class Main {

  public static void main(String[] args) throws Exception{

    Class cls = Class.forName("java.lang.String");
    System.out.println("Fields =");

    // returns the array of Field objects representing the public fields
    Field f[] = cls.getFields();
    for (int i = 0; i < f.length; i++) {
      System.out.println(f[i]);
    }

  }
}

The code above generates the following result.