java 内部类和局部变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10338708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inner class and local variables
提问by NINCOMPOOP
Why do I need to declare a local variable
as final
if my Inner class
defined within the method needs to use it ?
为什么我需要声明 alocal variable
就final
好像我Inner class
在方法中定义的需要使用它一样?
Example :
例子 :
class MyOuter2 {
private String x = "Outer2";
void doStuff() {
final String y = "Hello World";
final class MyInner {
String z = y;
public void seeOuter() {
System.out.println("Outer x is "+x);
System.out.println("Local variable is "+y);
MyInner mi = new MyInner();
mi.seeOuter();
}
}
}
}
}
Why the String y
needs to be a final constant ? How does it impact ?
为什么 Stringy
需要是最终常量?它是如何影响的?
采纳答案by ControlAltDel
The answer is the two are in different scopes. So that variable could change before the inner class accesses it. Making it final prevents that.
答案是两者在不同的范围内。因此该变量可以在内部类访问它之前更改。将其设为 final 可以防止这种情况。
回答by mprabhat
Local variables always live on the stack, the moment method is over all local variables are gone.
局部变量始终存在于堆栈中,方法结束的那一刻所有局部变量都消失了。
But your inner class objects might be on heap even after the method is over (Say an instance variable holds on to the reference), so in that case it cannot access your local variables since they are gone, unless you mark them as final
但是即使在方法结束后,您的内部类对象也可能在堆上(假设实例变量保留引用),因此在这种情况下,它无法访问您的局部变量,因为它们已经消失,除非您将它们标记为 final
回答by charles_ma
It's because the inner class inside a function actually makes a copy of the local variable because local variable may be destroyed after the function call while the instance of the local class still exists. To make the two copies of the local variable always have the same value(to make them seem identical), you have to declare the variable as final.
这是因为函数内部的内部类实际上复制了局部变量,因为局部变量可能在函数调用后被销毁,而局部类的实例仍然存在。要使局部变量的两个副本始终具有相同的值(使它们看起来相同),您必须将该变量声明为 final。
回答by Martijn Courteaux
I think this is to prevent that the programmer would make mistakes. This way, the compiler reminds the programmer that the variable will not be accessible anymore when you leave that method. This is critical with looping. Imagine this code:
我认为这是为了防止程序员犯错误。这样,编译器会提醒程序员,当您离开该方法时,将无法再访问该变量。这对于循环至关重要。想象一下这段代码:
public void doStuff()
{
InnerClass cls[] = new InnerClass[100];
for (int i = 0; i < 100; ++i)
{
cls[i] = new InnerClass()
{
void doIt()
{
System.out.println(i);
}
};
}
}
When the doIt()
method is called, what will be printed? i
changed while looping.
To prevent that confusion, you have to copy the variable to a local variable or create the variable final, but then you will be unable to loop.
当doIt()
方法被调用时,会打印什么?i
循环时改变。为了防止这种混淆,您必须将变量复制到局部变量或创建变量 final,但这样您将无法循环。
回答by abson
Your inner class is final and so you shouldn't be able to modify anything from inside your final entity.
您的内部类是最终的,因此您不应该能够从最终实体内部修改任何内容。
回答by Vishal
According to Java memory model use of final variable guarantees that the final variable are always initialized. We get error when we try to use local variable without initialization. using final guarantees the inner class that local variable which is been used is initialized.
根据 Java 内存模型,final 变量的使用保证了 final 变量总是被初始化。当我们尝试在没有初始化的情况下使用局部变量时会出错。使用 final 保证内部类被使用的局部变量被初始化。