java 有一个实例变量作为最终的点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2054327/
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
point of having an instance variable as final?
提问by ajsie
what is the point of having an instance variable as final?
将实例变量作为最终变量有什么意义?
isn′t it better to have that variable set as a static final variable then?
那么将该变量设置为静态最终变量不是更好吗?
cause if it can't be changed by any of the objects, then it's the same as a class (static) variable, right?
因为如果它不能被任何对象更改,那么它与类(静态)变量相同,对吗?
回答by Carl Manaster
Nope. staticmeans it's the same across all instances of the class. finalmeans it's not assignable after its initial assignment. So two instances could have different values for a non-static final variable.
不。 static意味着它在类的所有实例中都是相同的。 final意味着它在初始分配后不可分配。因此,对于非静态最终变量,两个实例可能具有不同的值。
There are many reasons you might want to make a variable final; one of the best is clarity. If I read a method and notice that the foo is final, I don't have to worry about where it's changing down below - because it isn't; it can't. I can make more changes to code with final variables with less concern ("did I change the value of foo before or after bar, and does it matter?") because I know that some variables aren't subject to change. It also focuses my attention on the variables that aresubject to change - and they're the ones that deserve more attention.
您可能希望将变量设为 final 的原因有很多;最好的之一是清晰度。如果我阅读了一个方法并注意到 foo 是最终的,我就不必担心它在下面的位置发生变化——因为它不是;它不能。我可以对带有最终变量的代码进行更多更改,而不必担心(“我在 bar 之前还是之后更改了 foo 的值,这有关系吗?”),因为我知道某些变量不会更改。它还侧重于这两个变量我的注意力都可能发生变化-他们是值得更多的关注的人。
回答by Ondra ?i?ka
Two reasons:
两个原因:
1) From the class design view, it allows a programmer to rely on the fact that the field will not change since instantiation - so called "immutable object" (where applied to class members, not the object's reference). Java tutorial says:
1) 从类设计的角度来看,它允许程序员依赖这样一个事实,即字段自实例化后不会改变——所谓的“不可变对象”(应用于类成员,而不是对象的引用)。Java教程说:
Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state.
不可变对象在并发应用程序中特别有用。因为它们不能改变状态,所以它们不会被线程干扰破坏或在不一致的状态下被观察到。
Immutable objects are a cornerstone of various programming styles, e.g. pure functional programming.
不可变对象是各种编程风格的基石,例如纯函数式编程。
2) The second reasons is JVM optimizations. If all fields are final, then JVM knows the object's state can't be changed, and thus it can make many optimizations, e.g. ommiting thread safety checks etc.
2)第二个原因是JVM优化。如果所有字段都是最终的,那么 JVM 知道对象的状态不能改变,因此它可以进行许多优化,例如省略线程安全检查等。
回答by Tom Hawtin - tackline
I guess you are thinking about simple case such as:
我猜您正在考虑简单的情况,例如:
private final int num = 3;
That can be better written as:
可以更好地写成:
private static final int NUM = 3;
However, often you will have references to objects that are mutable, and therefore cannot be shared between classes:
但是,通常您会引用可变的对象,因此不能在类之间共享:
private final List<Node> children = new ArrayList<Children>();
Or perhaps, a value passed into or derived in the constructors:
或者,一个传入或派生在构造函数中的值:
public final class MyThing {
private final String name;
public MyThing(String name) {
this.name = name;
}
[...]
}
Note: finalfields can be assigned in constructors (or instance initialiser), not just as part of the declaration.
注意:final字段可以在构造函数(或实例初始化器)中分配,而不仅仅是作为声明的一部分。
回答by Roy Tang
You can assign it a value that is specific to the object instance, which you can't do with a static class variable.
您可以为其分配一个特定于对象实例的值,而使用静态类变量则无法做到这一点。
回答by xiaokaoy
class A{
public final int x;
A(int arg){
x = arg; // This is legal !!!
}
}
public class HelloWorld{
public static void main(String []args){
A a = new A(1);
A b = new A(2);
System.out.printf("a.x = %d, b.x = %d", a.x, b.x);
}
}
回答by Ignacio Vazquez-Abrams
It prevents other programmers from doing stupid things that they shouldn't try doing anyway...
它可以防止其他程序员从做愚蠢的事情,他们不应该尝试做呢?

