Retrieve only static fields declared in Java class

后端 未结 4 1215

I have the following class:

public class Test {
    public static int a = 0;
    public int b = 1;
}

Is it possible to use reflection to ge

4条回答
  •  渐次进展
    2020-11-29 23:55

    You can do it like this:

    Field[] declaredFields = Test.class.getDeclaredFields();
    List staticFields = new ArrayList();
    for (Field field : declaredFields) {
        if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
            staticFields.add(field);
        }
    }
    

提交回复
热议问题