简体   繁体   English

在Immutable类中创建一个字段`Final`?

[英]Making a field `Final` in an Immutable class?

Recently I attended an interview. 最近我参加了一次采访。

I was asked how you can make a class Immutable . 有人问我怎样才能成为一个Immutable的课程。

I told him the answer. 我告诉他答案。

But then he asked me why the fields are final ? 但后来他问我为什么这些领域是final

I answered so that the user doesn't accidentally change the value of the field and it will give compiler error if he does so. 我回答说,用户不会意外地更改字段的值,如果他这样做,将给编译器错误。

Now he ask me there is a immutable class with only getter methods. 现在他问我有一个只有getter方法的immutable类。

Then in this class what is the use of final ? 然后在这个类有什么用的final

I was not able to answer. 我无法回答。 He told there is a reason behind that. 他说这背后有一个原因。

Can somebody explain? 有人可以解释一下吗?

From Effective Java: 来自Effective Java:

Make all fields final. 让所有领域最终。 This clearly expresses your intent in a manner that is enforced by the system. 这清楚地以系统强制执行的方式表达您的意图。 Also, it is necessary to ensure correct behavior if a reference to a newly created instance is passed from one thread to another without synchronization, as spelled out in the memory model. 此外,如果对新创建的实例的引用在没有同步的情况下从一个线程传递到另一个线程,则必须确保正确的行为,如内存模型中所述。

Now he ask me there is a immutable class with only getter methods. 现在他问我有一个只有getter方法的不可变类。

Then in this class what is the use of final? 然后在这个班级中有什么用途?

Only getter methods do not ensure that a class is immutable. 只有getter方法不能确保类是不可变的。 If you expose the internal state than a client can change the class's state. 如果暴露内部状态而不是客户端可以更改类的状态。

Immutable means that you can not change an objects state once it is created. 不可变意味着一旦创建对象状态就无法更改它。

Making all fields final and providing only getters will not make it immutable out of the box. 使所有领域成为最终并且仅提供吸气剂将不会使其永远不可改变。 Imagine the following code: 想象一下以下代码:

public class MyString {
    private final char[] content;

    public MyString(String str){
         this.content = str.toCharArray();
    }

    public char[] getContent(){
        return this.content; // internal state exposed. You should return a copy.
    }
}

This class has only final fields and only getter methods but is still mutable. 这个类只有最终字段,只有getter方法,但仍然是可变的。 Imagine this client code: 想象一下这个客户端代码:

MyString myString = new MyString("test");
myString.getContent()[0] = 'f';

Now he ask me there is a immutable class with only getter methods. 现在他问我有一个只有getter方法的不可变类。

Then in this class what is the use of final? 然后在这个班级中有什么用途?

The use of final is to express your intention with java's language features and therefore enforce them by the compiler. final的用法是用java的语言特性表达你的意图,因此编译器强制执行它们。

So making a variable final is good for primitive types, but you must take care if the variable is a reference. 因此,创建变量final对于原始类型是有益的,但是如果变量是引用,则必须注意。 In this case you must either ensure that 在这种情况下,您必须确保这一点

  • the object you are referencing is also immutable or 您引用的对象也是不可变的或
  • only your instance has access to that object. 只有您的实例才能访问该对象。

The later can only be ensured by the programmer. 后者只能由程序员确保。 So take extra care when you return references. 因此,在返回引用时要格外小心。

Well, immutability is of course achieved in the way that an object is used, rather than by enforcement. 那么,不变性当然是以使用对象的方式实现的,而不是通过强制实现的。 You could always change a final field's value with Reflection. 您总是可以使用Reflection更改最终字段的值。

The use of it is to allow the compiler to prevent you from breaking immutability, as well as to denote the need for immutability (such as when you use an inner class that uses a method-local reference). 它的使用是允许编译器阻止您破坏不变性,以及表示需要不变性(例如当您使用使用方法本地引用的内部类时)。

'final'作为关键字的名称建议意味着最终关键字所附加的属性不能更改(就值而言)换句话说它的行为就像一个常量。如果字段不是'final'那么在本地方法中你是可以改变字段的值。

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

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