[英]Why Netbeans suggests making variable final
So i have downloaded new Netbeans 8
version some time ago. 所以我前段时间下载了新的
Netbeans 8
版本。 In this new version everytime when i declare private
variable and assign to it value only once netbeans
suggests me to make field final because is never changed. 在这个新版本中,每当我声明
private
变量并仅为其分配值时, netbeans
建议我将字段设为最终,因为永远不会更改。
Ok I understand what final
variable is but **whats the advantage in declaring private final int x=3;
好的,我明白
final
变量是什么,但**在声明private final int x=3;
什么优势private final int x=3;
variable aka constant over private int x=3;
变量aka常量超过
private int x=3;
and never change it ** 永远不要改变它**
i thought about 我想过
Any clarification will be appreciated 任何澄清将不胜感激
Netbeans detects that the variable is actually probably a constant, and suggest for you to declare it as such. Netbeans检测到变量实际上可能是常量,并建议您将其声明为常量。 This has the advantage of preventing future bugs where this variable is accidentally modified.
这样做的好处是可以防止意外修改此变量的未来错误。
Also the final also documents the intention of making this variable a constant, together with the usual notation of defining the constant with all uppercase, which helps understand it's purpose in the program to other developers maintaining the code in the future. 最后,最后还记录了使这个变量成为常量的意图,以及通常用大写字母定义常量的符号,这有助于理解程序中其他开发人员在未来维护代码的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.