Can I access the static variables of the 'Class' Object?
Yes. Just use Class.getDeclaredFields()
(or Class.getDeclaredField(String)
) as normal, and to get the values, use the Field.getXyz()
methods, passing in null
for the obj
parameter.
Sample code:
import java.lang.reflect.Field;
class Foo {
public static int bar;
}
class Test {
public static void main(String[] args)
throws IllegalAccessException, NoSuchFieldException {
Field field = Foo.class.getDeclaredField("bar");
System.out.println(field.getInt(null)); // 0
Foo.bar = 10;
System.out.println(field.getInt(null)); // 10
}
}