使用反射访问静态最终变量
我有一个带有静态变量的 Java 类
package com.mytest
public class MyClass{
public static final TextClass TEXT_CLASS = new TextClass();
}
如何使用反射访问对象 TEXT_CLASS
?
(我有字符串 "com.mytest.MyClass.TEXT_CLASS"
。我需要访问该对象。)
I have a Java class with a static variable
package com.mytest
public class MyClass{
public static final TextClass TEXT_CLASS = new TextClass();
}
How can I access the object TEXT_CLASS
using reflection?
(I have the string "com.mytest.MyClass.TEXT_CLASS"
. I need to access the object.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问静态字段的方式与普通字段完全相同,只是您不需要向
Field.get()
方法传递任何参数(可以传递 null)。试试这个:
Accessing static fields is done exactly the same way as normal fields, only you don't need to pass any argument to
Field.get()
method (you can pass a null).Try this: