java中的最终对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3271287/
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
final object in java
提问by Manu
Possible Duplicate:
When to use final
可能的重复:
何时使用 final
What is the use of declaring final
keyword for objects? For example:
final
对象
的声明关键字有什么用?例如:
final Object obj = new myclass();
采纳答案by S73417H
Using the "final" keyword makes the the variable you are declaring immutable. Once initially assigned it cannot be re-assigned.
使用“final”关键字使您声明的变量不可变。一旦最初分配,就不能重新分配。
However, this does not necessarily mean the state of the instance being referred to by the variable is immutable, only the reference itself.
然而,这并不一定意味着变量引用的实例的状态是不可变的,只是引用本身是不可变的。
There are several reasons why you would use the "final" keyword on variables. One is optimization where by declaring a variable as final allows the value to be memoized.
在变量上使用“final”关键字有几个原因。一种是优化,其中通过将变量声明为 final 允许该值被记忆。
Another scenario where you would use a final variable is when an inner class within a method needs to access a variable in the declaring method. The following code illustrates this:
另一种使用 final 变量的情况是方法中的内部类需要访问声明方法中的变量。以下代码说明了这一点:
public void foo() {
final int x = 1;
new Runnable() {
@Override
public void run() {
int i = x;
}
};
}
If x is not declared as "final" the code will result in a compilation error. The exact reason for needing to be "final" is because the new class instance can outlive the method invocation and hence needs its own instance of x. So as to avoid having multiple copies of a mutable variable within the same scope, the variable must be declared final so that it cannot be changed.
如果 x 未声明为“final”,则代码将导致编译错误。需要“final”的确切原因是因为新的类实例可以比方法调用更有效,因此需要它自己的 x 实例。为了避免在同一范围内有多个可变变量的副本,变量必须声明为 final 以便不能更改。
Some programmers also advocate the use of "final" to prevent re-assigning variables accidentally where they should not be. Sort of a "best practice" type rule.
一些程序员还提倡使用“final”来防止意外地在不应该的地方重新分配变量。某种“最佳实践”类型规则。
回答by Darin Dimitrov
It declares a variable that cannot be modified once initialized. Sort of readonly.
它声明了一个一旦初始化就不能修改的变量。有点只读。
回答by Alterscape
Final variables cannot be reassigned. If they're class- or instance-scope, they must be assigned when an object is constructed. One use: constants you don't want anyone else using your to change, e.g. string or int constants (though enums do this better in 1.5+). Also, if you want to reference a method-scope variable from an anonymous method you define inside that method, it must be final, so Java doesn't have to deal with what would happen if your code reassigned that variable later, after it created the local stack frame/scope for the internal anonymous method. There's talk of including some form of closure in Java 7 which might avoid this restriction.
最终变量不能重新分配。如果它们是类作用域或实例作用域,则必须在构造对象时分配它们。一种用途:您不希望其他人使用您更改的常量,例如 string 或 int 常量(尽管枚举在 1.5+ 中做得更好)。此外,如果您想从在该方法中定义的匿名方法引用方法范围变量,它必须是最终的,因此 Java 不必处理如果您的代码稍后重新分配该变量会发生什么,在它创建之后内部匿名方法的本地堆栈帧/范围。有人谈到在 Java 7 中包含某种形式的闭包可能会避免这种限制。
I may have botched the terminology a bit -- sorry! but that's the general idea.
我可能在术语上有点拙劣——抱歉!但这是总体思路。
回答by Ishtar
Another use of final to prevent accidentally changing it:
final 的另一个用途是防止意外更改它:
public String foo(int a) {
final String s;
switch(a) {
case 0:
s="zero";
break;
case 1:
s="one";
//forgot break;
default:
s="some number";
}
return s;
}
This will not compile since if a==1, s would be assigned twice, because of the missing break. Final can help in nasty switch statements.
这不会编译,因为如果 a==1,s 将被分配两次,因为缺少中断。Final 可以帮助处理讨厌的 switch 语句。