java中final局部变量的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18225572/
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
Use of final local variables in java
提问by Aniket Thakur
I was wondering is there any usability of using final local variables. Variables are not overridden anyway when inheritance comes into picture. For example a simple code as below
我想知道使用最终局部变量是否有任何可用性。当继承出现时,无论如何都不会覆盖变量。例如一个简单的代码如下
public static void main(String args[]) {
final String data = "Hello World!";
System.out.println(data);
}
The example is quite simple one and may not be a relevant code but the question is more generic.I have seen a lot of codes(all incorporated in main function which have final local variables) Is there any usability of declaring local variables as final other than that they cannot be edited in the same function itself?
该示例非常简单,可能不是相关代码,但问题更通用。我已经看到很多代码(都包含在具有最终局部变量的主函数中)是否有任何将局部变量声明为最终其他的可用性比它们不能在同一个功能本身中编辑?
采纳答案by Jon Skeet
Firstly, the part about variables being "overridden" - final
has two very different meanings. For classes and methods, it's about inheritance; for variables it's about being read-only.
首先,关于变量被“覆盖”的部分 -final
有两种截然不同的含义。对于类和方法,它是关于继承的;对于变量,它是只读的。
There's one important "feature" of final local variables: they can be used in local (typically anonymous) inner classes. Non-final local variables can't be. That's the primary use of final
for local variables, in my experience.
final 局部变量有一个重要的“特性”:它们可以在局部(通常是匿名的)内部类中使用。非最终局部变量不能。final
根据我的经验,这是局部变量的主要用途。
public void foo() {
final String x = "hello";
String y = "there";
Runnable runnable = new Runnable() {
@Override public void run() {
System.out.println(x); // This is valid
System.out.println(y); // This is not
}
};
runnable.run();
}
Note that as a matter of style, some people like to use final
even when they're notcapturing the variable in a local inner class. I'd certainly be comfortable with final
being the default, but a different modifier for "non-final", but I find that adding the modifier explicitly everywhere is too distracting.
请注意,就风格而言,有些人final
即使没有在本地内部类中捕获变量也喜欢使用。我当然愿意成为final
默认值,但对于“非最终”使用不同的修饰符,但我发现在任何地方都明确添加修饰符会让人分心。
回答by John Smith
final local variables may be accessed from anonymous inner subclasses, whereas non final local variables may not.
final 局部变量可以从匿名内部子类访问,而非 final 局部变量则不能。
回答by Subhrajyoti Majumder
yes there is, a small expample where we could normally use it -
是的,有一个小例子,我们通常可以使用它 -
Snippet:
片段:
final Boolean data = Boolean.TRUE;
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if (data ) {
....
}
}
});
Its a example of usage with anonymous inner classes. If you want to use your local variable inside inner calss, you have to make final
.
它是匿名内部类的使用示例。如果你想在内部类中使用你的局部变量,你必须使final
.
回答by Krushna
Yes, the usability is :- local final variable are accessible by the method inner class. Since local variables live on the stack and exist only for lifetime of the method but Inner class object may live longer so inner class can't access any non final local variable. But once the variable is final then the inner class sure that the value won't change so it keep a private copy of the final local variable for use.
是的,可用性是:- 方法内部类可以访问局部最终变量。由于局部变量存在于堆栈中并且仅存在于方法的生命周期内,但内部类对象可能存活更长时间,因此内部类无法访问任何非最终局部变量。但是一旦变量是最终的,那么内部类就会确保该值不会改变,因此它会保留最终局部变量的私有副本以供使用。
回答by Tala
You might use final
keyword for readability and ensuring it is not modified or in anonymous classes like here:
您可以使用final
关键字来提高可读性并确保它不被修改或在匿名类中使用,如下所示:
final String s = "hi";
new Thread(new Runnable() {
@Override
public void run() {
// can use s variable here
}
}).start();
回答by Sajal Dutta
It tells the other programmers that whoever wrote it knew that the value of datashouldn't change once assigned. It's a good habit in my opinion especially declaring the parameter variables as final.
它告诉其他程序员,编写它的人都知道数据的值一旦分配就不应更改。我认为这是一个好习惯,尤其是将参数变量声明为final。
回答by Algiz
Perfect answer Jon Skeet but there is another (little) benefit of final variables.
完美的答案 Jon Skeet 但最终变量还有另一个(小)好处。
The compiler will ensure the final variable is set once and only once.
编译器将确保最终变量设置一次且仅一次。
Let's say you have to initialize a variable and the computation of the value is complex (multiple if-then-else within if-then-else). You might write code that does not initialize the variable in some condition (or initialize it twice). Of course, this is a bug. Making the variable final will allow the compiler to check the final variable is set once and only once. So you will know quickly if you have a bug.
假设您必须初始化一个变量并且该值的计算很复杂(在 if-then-else 中多个 if-then-else)。您可能会编写在某些条件下不初始化变量的代码(或将其初始化两次)。当然,这是一个错误。将变量设为 final 将允许编译器检查最终变量是否设置一次且仅一次。所以你会很快知道你是否有错误。
As I said, little benefit but still handy.
正如我所说,几乎没有什么好处,但仍然很方便。
回答by Premraj
Use of final local variables in java
java中final局部变量的使用
final
fields, parameters, and local variables are read-only(means the object's identity, not its state).
final
字段、参数和局部变量是只读的(意味着对象的身份,而不是它的状态)。