最终字段可能尚未/已经初始化 [英] The final field may not/already have been initialized
本文介绍了最终字段可能尚未/已经初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何处理静态最终字段初始化程序引发检查异常
Possible Duplicate:
How to handle a static final field initializer that throws checked exception
在此示例中,我收到错误消息空白的最终字段myClass可能未已初始化:
In this example, I get the error The blank final field myClass may not have been initialized:
private final static MyClass myClass; // <-- error
static {
try {
myClass = new MyClass(); // <-- throws exception
myClass.init();
} catch (Exception e) {
// log
}
}
在该示例中,我收到错误消息 myClass的最后一个字段可能已已经已分配:
In that example, I get the error The final field myClass may already have been assigned:
private final static MyClass myClass;
static {
try {
myClass = new MyClass(); // <-- throws exception
myClass.init();
} catch (Exception e) {
myClass = null; // <-- error
// log
}
}
对此问题有什么解决办法吗?
In there any solution to that issue?
推荐答案
private final static MyClass myClass;
static {
MyClass my;
try {
my = new MyClass();
my.init();
} catch (Exception e) {
my = null;
// log
}
myClass = my; //only one assignment!
}
这篇关于最终字段可能尚未/已经初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文