Java 在增强的 for 循环中对循环变量使用 final 的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18019582/
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
What is the purpose of using final for the loop variable in enhanced for loop?
提问by Krithika Vittal
I understand how the below statement works.
我了解以下语句的工作原理。
for(final Animal animal : animalList){
//do some function
}
But what is the purpose of using the final keyword here ?
但是在这里使用 final 关键字的目的是什么?
采纳答案by Stephen C
There are two possible reasons to do this:
这样做有两个可能的原因:
It could simply be a way to avoid changing the loop variable accidentally in the loop body. (Or to documentthe fact that the loop variable is not going to be changed.)
It could be done so that you can refer to the loop variable in an anonymous inner class. For example:
for(final Animal animal : animalList){ executor.submit(new Runnable(){ public void run() { animal.feed(); } }); }It is a compilation error if you leave out the
finalin this example.UPDATEit is not a compilation error in Java 8 and later versions. The non-local variable is now only required to be effectively final. In simple terms, that means that the variable is not assigned to (using an assignment operator or a pre/post increment or decrement operator) after the initial declaration / initialization.
这可能只是一种避免在循环体中意外更改循环变量的方法。(或者记录循环变量不会改变的事实。)
可以这样做,以便您可以在匿名内部类中引用循环变量。例如:
for(final Animal animal : animalList){ executor.submit(new Runnable(){ public void run() { animal.feed(); } }); }如果您
final在本示例中省略了,则会出现编译错误。更新它不是 Java 8 和更高版本中的编译错误。非局部变量现在只需要有效地 final。简单来说,这意味着在初始声明/初始化之后,变量不会被赋值(使用赋值运算符或前后自增或自减运算符)。
回答by Tala
Adding finalkeyword makes no performance difference here. It's just needed to be sure it is not reassigned in the loop.
添加final关键字在这里没有性能差异。只需要确保它不会在循环中重新分配。
To avoid this situation which can lead to confusions.
为了避免这种可能导致混淆的情况。
for(Animal animal : animalList){
//do some function
animal = anotherAnimal;
// use animal variable here
}
You could also need to use this variable in anonymous function inside the loop
您可能还需要在循环内的匿名函数中使用此变量
回答by Suresh Atta
It's another java best practise.
这是另一个 Java 最佳实践。
The finaldeclaration causes Javacompilers to reject any assignments made to the loop variable.
该final声明会导致Java编译器拒绝对循环所做的任何分配variable。
When ever you have a chance, Declare all enhanced for statement loop variables final
当你有机会时,将所有增强的 for 语句循环变量声明为 final
回答by Chris Bode
It simply means that the value of animalcannot change once it is set by the for loop. This may be required if you're going to reference animalwithin an anonymous class at some point in the loop.
它只是意味着animal一旦被 for 循环设置,值就不能改变。如果您要animal在循环中的某个点引用匿名类,则可能需要这样做。
Even if it isn't explicitly needed, it is good practice to make variables that will not change final. It can help you catch mistakes, and makes the code more self-documenting.
即使它不是明确需要的,制作不会改变 final 的变量也是一种很好的做法。它可以帮助您发现错误,并使代码更具自文档性。
回答by kan
The code is a syntax sugar for this equivalent code:
该代码是此等效代码的语法糖:
for (final Iterator<Animal> iterator = animals.iterator(); iterator.hasNext(); ) {
final Animal animal = iterator.next();
}
I think it is answers the question.
我认为这是回答问题。

