Java Class.getDeclaredFields()
Syntax
Class.getDeclaredFields() has the following syntax.
public Field [] getDeclaredFields() throws SecurityException
Example
In the following code shows how to use Class.getDeclaredFields() method.
import java.lang.reflect.Field;
//from w w w . j av a2 s . c om
public class Main {
public static void main(String[] args) {
try {
MyClass c = new MyClass();
Class cls = c.getClass();
// returns the array of Field objects
Field[] fields = cls.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println("Field = " + fields[i].toString());
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
class MyClass {
public MyClass() {
// no argument constructor
}
public MyClass(long l, int i) {
this.l = l;
this.i = i;
}
long l = 77688;
int i = 3;
}
The code above generates the following result.