简体   繁体   English

有一个实例变量作为最终的点?

[英]point of having an instance variable as final?

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?因为如果它不能被任何对象更改,那么它与类(静态)变量相同,对吗?

Nope.不。 static means it's the same across all instances of the class. static意味着它在类的所有实例中都是相同的。 final means it's not assignable after its initial assignment. final意味着它在初始分配后不可分配。 So two instances could have different values for a non-static final variable.因此,对于非静态最终变量,两个实例可能具有不同的值。

There are many reasons you might want to make a variable final;您可能希望将变量设为 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;如果我阅读了一个方法并注意到 foo 是最终的,我不必担心它在下面的位置发生变化 - 因为它不是; 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.我可以对带有 final 变量的代码进行更多更改而不必担心(“我在 bar 之前或之后更改了 foo 的值,这有关系吗?”),因为我知道某些变量不会更改。 It also focuses my attention on the variables that are subject to change - and they're the ones that deserve more attention.它还侧重于那些可能发生变化的变量我的注意-他们是值得更多的关注的人。

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). 1) 从类设计的角度来看,它允许程序员依赖于字段自实例化后不会改变的事实——所谓的“ 不可变对象”(应用于类成员,而不是对象的引用)。 Java tutorial says: 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, eg pure functional programming.不可变对象是各种编程风格的基石,例如纯函数式编程。

2) The second reasons is JVM optimizations. 2)第二个原因是JVM优化。 If all fields are final, then JVM knows the object's state can't be changed, and thus it can make many optimizations, eg ommiting thread safety checks etc.如果所有字段都是最终的,那么 JVM 知道对象的状态不能改变,因此它可以进行许多优化,例如省略线程安全检查等。

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: final fields can be assigned in constructors (or instance initialiser), not just as part of the declaration.注意: final字段可以在构造函数(或实例初始化器)中分配,而不仅仅是作为声明的一部分。

您可以为其分配一个特定于对象实例的值,而使用静态类变量则无法做到这一点。

它可以防止其他程序员从做愚蠢的事情,他们不应该想试试做...

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);
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM