java Java中传递的最终变量是否在另一侧保持最终?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2297226/
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
Does a passed final variable in Java stay final on the other side?
提问by froadie
I've just come across some code that's confusing me slightly; there are really 2 variations that I'd like to clarify.
我刚刚遇到了一些让我有点困惑的代码;我真的想澄清两种变化。
Example 1:
示例 1:
public String getFilepath(){
final File file = new File(this.folder, this.filename);
return file.getAbsolutePath();
}
What would be the purpose of declaring file"final"? Since Java primitives are passed by value, and getAbsolutePath()is just returning a String, the variable won't be finalon the other side (calling method), will it? And since the filevariable only exists within the scope of these 2 lines, I can't really see any purpose of the finalkeyword. Is there something I'm missing? Anyone see a reason to do this?
声明file“ final”的目的是什么?由于 Java 原语是按值传递的,并且getAbsolutePath()只是返回 a String,变量不会final在另一侧(调用方法),是吗?而且由于file变量只存在于这两行的范围内,我真的看不出final关键字的任何用途。有什么我想念的吗?有人看到这样做的理由吗?
Example 2:
示例 2:
public String getFilepath(){
final File file = new File(this.folder, this.filename);
return file;
}
Since here the actual object is being returned... Does that mean the filevariable will be constant/finalon the other side...? It doesn't seem to make sense.
由于这里返回的是实际对象......这是否意味着file变量将是常量/final在另一侧......?这似乎没有意义。
In general, it seems to me that you pass a variable, withoutit's access type. As in, I can have a privatevariable in a function with a publicget function that returns it - but the variable that receives it by calling the function has to specify an access modifier. So if it specifies public, the returned variable will be publicin that scope. If it specifies private, the returned variable will be privatein that scope. Is there a difference with final? Is the "constancy" of a variable something that can be passed? This strikes me as rather improbable, considering what I know of Java.
一般来说,在我看来,您传递了一个变量,而没有它的访问类型。就像在,我可以private在一个函数中有一个变量,它有一个public返回它的get 函数 - 但是通过调用函数接收它的变量必须指定一个访问修饰符。因此,如果它指定了public,则返回的变量将public在该范围内。如果指定private,则返回的变量将private在该范围内。有区别final吗?变量的“恒定性”是可以传递的吗?考虑到我对 Java 的了解,这让我觉得不太可能。
Or am I missing the point entirely and there's some other purpose of the finalkeyword in the above code?
还是我完全没有final抓住重点,上面代码中的关键字还有其他用途?
Edit:
编辑:
I checked back with the original developer who wrote the code, and he said he only put the finalkeyword in because he had originally thought the method would be a lot longer and wanted to ensure that the file stayed constant throughout. He also said that he generally declares variables that should not be changed as final, as a rule across the board and sort of on principle - a point that both the answers below mentioned. So it seems I was reading too much into a simple extra keyword included for standards reasons. Thanks everyone!
我与编写代码的原始开发人员核对,他说他只输入final关键字是因为他最初认为该方法会更长,并希望确保文件始终保持不变。他还说,他通常将不应更改的变量声明为final,作为全面的规则和原则上 - 下面的两个答案都提到了这一点。因此,出于标准原因,我似乎对包含的一个简单的额外关键字读得太多了。感谢大家!
采纳答案by Dancrumb
Some people might tell you that there's a performance benefit to using final, but that is, in no way, conclusively proven.
有些人可能会告诉您,使用 final 有性能优势,但这绝不是最终证明。
The primary benefit of the finalkeyword is for the programmer to indicate that a class, method, or field should not be changed.
final关键字的主要好处是让程序员指示不应更改类、方法或字段。
Bear in mind that declaring a variable finaldoes not make the referenced object immutable. It just means that the variable cannot have its value reassigned. You can still run methods of the variable filethat could change the Fileobject internally.
请记住,声明变量final不会使引用的对象不可变。这只是意味着变量不能重新分配其值。您仍然可以运行file可以在File内部更改对象的变量方法。
In the two methods you give, I see no value in making the filevariable final. Some code conventions advocate making all variable finalunless the need to be modified. Some people don't subscribe to that. I consider it a variation on the precautionary principle.
在您提供的两种方法中,我认为创建file变量没有任何价值final。一些代码约定主张将所有变量都设置为final除非需要修改。有些人不同意。我认为这是对预防原则的一种变体。
回答by Kris
finalin this case just means that the local reference filewill be immutable. It has no meaning outside the method. Some coding conventions advocate having all variables final unless they need to be mutable so you'll see code like that when someone is following such guidelines.
final在这种情况下只是意味着本地引用file将是不可变的。它在方法之外没有任何意义。一些编码约定主张将所有变量设为 final,除非它们需要可变,因此当有人遵循此类准则时,您会看到类似的代码。

