简体   繁体   English

将不可变对象中的变量定义为final是仅出于可读性考虑吗?

[英]Is it only for readability that the variables in immutable object defined as final?

In immutable objects making the variables private will restrict the access from external world. 在不可变的对象中,将变量设为私有将限制来自外部世界的访问。 What is the need for making the variables final ? 使变量最终化有什么需要? Is the reason is only to make it readable ? 原因仅仅是为了使其可读性吗? that if the variables are final it means the object is immutable ? 那如果变量是最终的,就意味着对象是不可变的? Is there any theoretical reason behind it ? 它背后有什么理论上的原因吗?

You do not mark variable as final, but a reference to a variable. 您不会将变量标记为最终变量,而是将其标记为变量。 It does not guarantee immutability at all. 它根本不能保证不变性。 It merely guarantees that once assigned final reference can't be reassigned + gives you thread safety guarantee. 它只是保证一旦分配了最终参考就不能重新分配+为您提供线程安全保证。

Consider an example like this: 考虑这样的示例:

class A {
 private int someVariable = 5;

public void setSomeVariable(int someVariable) {
this.someVariable = someVariable;
}

class B {
 private final A a;

 public B(A a) {
  this.a = a;
 }
}

public class C { 
 public static void main(String[] args) {
   A someA = new A();
   B someB = new B(someA);

   //field a in B is final but I can do this:
   someA.setSomeVariable(6); // by having a reference to someA I have modified someB though it is a final field



 }

You seem to have a few misconceptions in your understanding. 您的理解似乎有些误解。 Let's go over them one by one. 让我们一一介绍。

if the variables are final it means the object is immutable 如果变量是最终变量,则表示对象是不可变的

This is not right. 这是不对的。 Immutability and marking something as final are completely different concepts. 不变性和将事物标记为final事物是完全不同的概念。

Immutability makes an object's properties (fields) unchangeable once the object is created. 创建对象后,不变性使对象的属性(字段)不变。 final variables on the other hand, is something that you cannot change the value of after the first assignment. 另一方面, final变量是您在第一次分配后不能更改其值的东西。

Let's consider this immutable object's class: 让我们考虑这个不可变对象的类:

class MyClass {
    private int myField;
    public int getMyField() { return myField; }

    public MyClass(int myField) { this.myField = myField; }
}

As you can see, myField cannot change upon initialisation. 如您所见, myField不能在初始化时更改。 It cannot be changed in any part of the code except the constructor. 除构造函数外,不能在代码的任何部分中更改它。 Let's create a variable of this: 让我们创建一个这样的变量:

MyClass obj = new MyClass(10);

As we know, obj.myField cannot be changed anymore at this point, but can obj be changed? 众所周知, obj.myField无法再更改obj.myField ,但是可以更改obj吗? Yes it can eg 是的,它可以例如

obj = null;

You can declare it final to make obj unchangeable: 您可以将其声明为final以使obj不可更改:

final MyClass obj = null;
obj = new MyClass(10); // error!

Now that you see the difference between final and immutability, let's move on to the benefits of both. 现在您已经看到了最终和不变性之间的区别,让我们继续介绍两者的好处。

Immutability and final have pretty much the same benefits. 不变性和final具有几乎相同的好处。 They both ensure that values that should stay the same will not be accidentally changed. 它们都确保应保持不变的值不会被意外更改。 It is more to "avoid mistakes" than to "improve readability". “避免错误”比“提高可读性”更多。

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

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