Is it possible to reflectively get all fields in a java interface? for example :
import java.lang.reflect.*;
public class field1 {
private double d;
public static final int i = 37;
String s = "testing";
public static void main(String args[]) {
try {
Class cls = Class.forName("field1");
Field fieldlist[] = cls.getDeclaredFields();
for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
System.out.println("name= " + fld.getName());
System.out.println("-----");
}
} catch (Throwable e) {
System.err.println(e);
}
}
}
FIXED!!
I forgot to add full classpath in Class.forName(“field1”); should be Class.forName(“com.x.y.z.field1”);
Certainly. The same code you have here should work on an interface. Bear in mind that any fields you get from an interface will be both
staticandfinal.