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.


//from   ww w .  j  av  a 2 s .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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable