The following class gets with reflection the field names of class Test:
public class MainClass {
public class Test {
public static final int field1 = 1;
public static final int field2 = 2;
public static final int field3 = 3;
}
public static void main(String[] args) {
Field[] fields = Test.class.getDeclaredFields();
for (Field field: fields) {
System.out.println(field.getName());
}
}
}
This prints:
field1
field2
field3
this$0
But I also need to print the initialization values (filtering this$0
), for example:
field1 - 1
field2 - 2
field3 - 3
There's a getInt(Object o)
method in Field
, but since there's no instance (the fields are static) I cannot use it. Any ideas?
Aucun commentaire:
Enregistrer un commentaire