沉思访问没有初始化的最后静态变量 [英] Reflectively accessing final static variable without initialization

查看:134
本文介绍了沉思访问没有初始化的最后静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用反射类访问了一堆最终静态公共整数的。这个类然而不具有构造 - 例如Android的R.id。我试图让所有的INT值对于这些,但是我似乎无法访问它由于这一事实,你不能创建类。我想可能延长它只是为了创建一个构造函数,但我不能确定这是明智的。有什么建议么?我不能修改R.id或R.array(至少我不应该我认为)。

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

推荐答案

这就是你需要:

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

这篇关于沉思访问没有初始化的最后静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆