[英]final variable might not have initialized
I have code like this within a method: 我在一个方法中有这样的代码:
final TouchableSpan touchableSpan = new TouchableSpan() {
@Override
public void onClick(View widget) {
this.setPressed(true);
String extravar = touchableSpan.getMyVar();
}
On this line String extravar = touchableSpan.getMyVar();
在这一行
String extravar = touchableSpan.getMyVar();
I get a warning that variable touchableSpan
might have not been initialized. 我得到一个警告,变量
touchableSpan
可能尚未初始化。 Why is there? 为什么?
This warning appeared when I added final
modifier. 当我添加
final
修饰符时出现此警告。 Before I had variable is access from inner class, needs to be declared final
. 在我
variable is access from inner class, needs to be declared final
之前variable is access from inner class, needs to be declared final
。
You first create an anonymous class and then assign it to the final variable. 首先创建一个匿名类,然后将其分配给最终变量。 Thus your
onClick
method theoretically might be called before final variable initialization. 因此理论上可以在最终变量初始化之前调用
onClick
方法。 Why not just using this.getMyVar()
? 为什么不直接使用
this.getMyVar()
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.