Reflectively accessing final static variable without initialization

主宰稳场 提交于 2020-01-03 04:39:06

问题


I am trying to access a bunch of final static public ints from a class using reflection. This class however doesn't have a constructor - e.g. the android R.id . I am trying to get all int values for these, however I can't seem to access it due to the fact that you can't create the class. I was thinking of possible extending it just to create a constructor, but I am unsure this is wise. Any suggestions? I can't modify R.id or R.array (at least I shouldn't I think).

Thanks in advanced! Jon


回答1:


That's all you need:

Field field = R.id.class.getField("some_var");
int value = field.getInt(null);



回答2:


This should help you.



来源:https://stackoverflow.com/questions/5406108/reflectively-accessing-final-static-variable-without-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!