Assuming these constants are in static fields:
import java.lang.reflect.*;
public class Reflect {
public static final String CONSTANT_1 = "1";
public static final String CONSTANT_2 = "2";
public static final String CONSTANT_3 = "3";
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("Reflect");
Field[] fields = clazz.getDeclaredFields();
for(Field f: fields) {
// for fields that are not visible (e.g. private)
f.setAccessible(true);
// note: get(null) for static field
System.err.printf("%s: %s\n",f, (String)f.get(null) );
}
}
}
The output is:
$ java Reflect
public static final java.lang.String Reflect.CONSTANT_1: 1
public static final java.lang.String Reflect.CONSTANT_2: 2
public static final java.lang.String Reflect.CONSTANT_3: 3
Note that to get the value of a static field, you supply null
as the arg.