为什么它说“不能引用在不同方法中定义的内部类中的非最终变量 i”?
我有按钮单击侦听器,在 onCreate() 方法中我有一个局部变量,例如
onCreate() {
super.onCreate();
int i = 10;
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
}
});
为什么 java 要求使我成为最终的?
I have button click listener and in onCreate()
method I have a local variable like
onCreate() {
super.onCreate();
int i = 10;
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
}
});
Why java asks for to make me final ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当 onCreate() 方法返回时,局部变量将从堆栈中清除,因此它们将不再存在。但匿名类对象 new View.OnClickListener() 引用了这些变量。因为这是错误的行为,所以 java 不允许你这样做。
最终确定后,它就成为常量。因此它存储在堆中并且可以在匿名类中安全地使用。
When the onCreate() method returns, your local variable will be cleaned up from the stack, so they won't exist anymore. But the anonymous class object new View.OnClickListener() references these variables. Of cause it's wrong behavior so java don't allow you to do this.
After it is final it becomes a constant. So it is storing in the heap and can be safely used in anonymous classes.
您的匿名内部类通过获取局部变量的副本来引用其封闭范围 - 如果您想更改匿名内部类中 int 的值,您需要做一些黑客操作:
Your anonymous inner class refers to its enclosing scope by taking copies of the local variables - if you want to change the value of an int in an anonymous inner class, you need to do some hackery:
因为您是在匿名内部类中访问它。你不能那样做。您可以将其设为最终的,然后从匿名内部类读取它,但随后您无法递增它。
选项:
我可能会这样做赞成第二个选项,除非我需要从其他地方获取
i
。老实说,我认为第三种选择有点令人讨厌。Because you're accessing it within an anonymous inner class. You can't do that. You can make it final and then read it from the anonymous inner class, but you then can't increment it.
Options:
AtomicInteger
or something like thatI would probably favour the second option unless I needed to get at
i
from anywhere else. I regard the third option as a bit of a nasty hack, to be honest.使变量
final
是必要的,因为在幕后,像这样的匿名内部类只是 编译为包含方法范围之外的嵌套类的语法糖。这意味着内部类无法访问该方法内部声明的任何变量,因此编译器会使用另一个技巧 - 并复制类的隐藏构造函数中的值。为了避免程序员因该副本未更新以匹配方法中变量的更改而感到困惑,它必须是最终的以确保不存在此类更改。
由于您的目标是拥有一个递增的整数,并且只有引用必须是最终的(对象本身不必是不可变的),因此您可以声明一个
final AtomicInteger i
,然后根据需要从打回来。Making the variable
final
is necessary because under the hood, anonymous inner classes like that are simply syntactic sugar that compile to a nested class outside the scope of the containing method.This means that none of the variables declared inside of the method are accessible to the inner class, so the compiler pulls another trick - and copies the value in a hidden constructor for your class. To avoid programmer confusion where this copy is not updated to match changes to the variable in the method, it must be final to make sure there are no such changes.
Since your goal here is to have an incrementing integer, and only the reference must be final (the object itself need not be immutable) you could declare a
final AtomicInteger i
and then increment it as you wish from the callback.由于您需要增加
i
变量,因此您无法将其确定为最终值。您可以将其设为班级成员。Since you need to increment the
i
variable you can't make it final. You could make it a class member instead.