简体   繁体   中英

Reflectively accessing final static variable without initialization

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 - eg 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

That's all you need:

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

应该对您有帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM