getting all static variables in a class into array/list

后端 未结 2 1258
余生分开走
余生分开走 2021-02-05 13:49

Bit of a wierd requirement.

public class DummyClass{
   public static final DummyClass var1;
   public static final DummyClass var2;
   public static final Dummy         


        
2条回答
  •  情话喂你
    2021-02-05 14:32

    You could use reflection:

    Field[] fields = DummyClass.class.getDeclaredFields();
    for (Field f : fields) {
        if (Modifier.isStatic(f.getModifiers()) && isRightName(f.getName())) {
            doWhatever(f);
        } 
    }
    

提交回复
热议问题