Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不可变对象的所有属性都必须是final吗?_Java_Immutability_Final_Java Memory Model - Fatal编程技术网

Java 不可变对象的所有属性都必须是final吗?

Java 不可变对象的所有属性都必须是final吗?,java,immutability,final,java-memory-model,Java,Immutability,Final,Java Memory Model,不可变对象的所有属性必须是final 据我所说不是。但我不知道我是否正确。不可变=不可变。因此,将属性设置为最终属性是一个好主意。如果不是一个对象的所有属性都被保护不被更改,我不会说这个对象是不可变的 但是,如果一个对象没有为其私有属性提供任何设置器,那么它也是不可变的。不可变对象(所有属性都是最终的)和有效不可变对象(属性不是最终的,但不能更改)之间的主要区别是安全发布 由于以下功能,您可以在多线程上下文中安全地发布不可变对象,而无需担心添加同步: 最后一个字段还允许程序员实现线程安全的不可变

不可变对象的所有属性必须是
final


据我所说不是。但我不知道我是否正确。

不可变=不可变。因此,将属性设置为最终属性是一个好主意。如果不是一个对象的所有属性都被保护不被更改,我不会说这个对象是不可变的


但是,如果一个对象没有为其私有属性提供任何设置器,那么它也是不可变的。

不可变对象(所有属性都是最终的)和有效不可变对象(属性不是最终的,但不能更改)之间的主要区别是安全发布

由于以下功能,您可以在多线程上下文中安全地发布不可变对象,而无需担心添加同步:

最后一个字段还允许程序员实现线程安全的不可变对象,而无需同步。线程安全的不可变对象被所有线程视为不可变的,即使使用数据竞争在线程之间传递对不可变对象的引用。这可以提供安全保证,防止不正确或恶意代码滥用不可变类。必须正确使用final字段以保证不变性

作为旁注,它还支持强制执行不可变性(如果您试图在类的未来版本中对这些字段进行变异,因为您忘记了它应该是不可变的,那么它将无法编译)


澄清

  • 使一个对象的所有字段为final并不能使其不变-您还需要确保(i)其状态不能更改(例如,如果该对象包含
    最终列表
    ,则在构造后不必执行任何变异操作(添加、删除…),以及(ii)在构造期间不允许
    转义
  • 一个有效的不可变对象在安全发布后是线程安全的
  • 不安全出版物示例:

    class EffectivelyImmutable {
        static EffectivelyImmutable unsafe;
        private int i;
        public EffectivelyImmutable (int i) { this.i = i; }
        public int get() { return i; }
    }
    
    // in some thread
    EffectivelyImmutable.unsafe = new EffectivelyImmutable(1);
    
    //in some other thread
    if (EffectivelyImmutable.unsafe != null
        && EffectivelyImmutable.unsafe.get() != 1)
        System.out.println("What???");
    
    理论上,这个程序可以打印什么。如果
    i
    是最终结果,那就不是法律结果


不可变对象在创建后不得以任何方式进行修改。当然,期末考试有助于实现这一目标。你保证他们永远不会改变但是如果对象中有一个数组是final呢?当然,引用是不可更改的,但元素是可更改的。请看我提出的几乎相同的问题:


简单地将对象声明为
final
并不能使其本质上不可变。例如:


您可以通过单独封装轻松保证不变性,因此无需:

但是,在某些情况下,您还必须通过封装来保证,因此这是不够的:

public-class-Womble{
私人最终名单卷心菜;
公共厕所(列出卷心菜){
这个。卷心菜=卷心菜;
}
公共卷心菜目录(){
卷心菜;
}
}
// ...
Womble w=新Womble(…);
//在您的设计中,这可能算作变异。(也可能不是。)
w、 获取卷心菜()。添加(“卷心菜”);
这样做是为了捕捉一些琐碎的错误,并清楚地表明您的意图,这不是一个坏主意,但是“所有字段都是最终的”和“类是不可变的”不是等价的语句。


例如,请参见
java.lang.String
的实现。字符串在Java中是不可变的,但字段
hash
不是最终字段(它在第一次调用
hashCode
时被延迟计算,然后被缓存)。但这是可行的,因为每次计算时,
hash
只能接受一个相同的非默认值。

String类是不可变的,但属性hash不是最终值

这是可能的,但有一些规则/限制,即每次访问可变属性/字段时,必须提供相同的结果

在String类中,hashcode实际上是在最后一个字符数组上计算的,如果String已构造,则该数组不会更改。因此,不可变类可以包含可变字段/属性,但它必须确保每次访问字段/属性时都会产生相同的结果

要回答您的问题,在一个不可变的类中不强制所有字段都是final


如需进一步阅读,请访问此处[blog]:

不需要,您可以通过将成员设置为非最终成员但为私有成员,并且除了在构造函数中之外不修改它们来实现相同的功能。不要为它们提供setter方法,如果它是一个可变对象,那么永远不要泄漏该成员的任何引用


请记住,将引用变量设为final只能确保它不会被重新分配不同的值,但您仍然可以更改该引用变量所指向的对象的各个属性。这是一个关键点。

好的,final只会使您无法重新分配字段。。final字段引用的对象可以是可变的。虽然我同意u的观点,但是如果一个对象的数组字段是final字段,那该怎么办呢?引用是不可变的,但值不是。例如,不可变的StringBuilder列表也是如此。这些对象被认为是线程安全的吗?一个具有非最终私有字段(在对象构造时初始化)的对象怎么会是线程不安全的?如果它们的所有字段都是最终字段,它们仍然可以安全发布,但这还不足以使它们成为线程安全的。在什么情况下它们会是线程不安全的?创建对象后,任何内容都无法更改。。。因此,我不理解它们是如何不安全的。@AdrienBrunelat在本例中,存在数据竞争,并且不能保证完全构造非空对象(即,字段可能仍然未初始化)。如果字段是最终字段,JLS保证即使通过数据竞争访问对象,它们也会被初始化(参见我回答中的引用)。我有一个问题:我们如何实现功能
import java.util.Date;

/**
* Planet is an immutable class, since there is no way to change
* its state after construction.
*/
public final class Planet {

  public Planet (double aMass, String aName, Date aDateOfDiscovery) {
     fMass = aMass;
     fName = aName;
     //make a private copy of aDateOfDiscovery
     //this is the only way to keep the fDateOfDiscovery
     //field private, and shields this class from any changes that 
     //the caller may make to the original aDateOfDiscovery object
     fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());
  }

  /**
  * Returns a primitive value.
  *
  * The caller can do whatever they want with the return value, without 
  * affecting the internals of this class. Why? Because this is a primitive 
  * value. The caller sees its "own" double that simply has the
  * same value as fMass.
  */
  public double getMass() {
    return fMass;
  }

  /**
  * Returns an immutable object.
  *
  * The caller gets a direct reference to the internal field. But this is not 
  * dangerous, since String is immutable and cannot be changed.
  */
  public String getName() {
    return fName;
  }

//  /**
//  * Returns a mutable object - likely bad style.
//  *
//  * The caller gets a direct reference to the internal field. This is usually dangerous, 
//  * since the Date object state can be changed both by this class and its caller.
//  * That is, this class is no longer in complete control of fDate.
//  */
//  public Date getDateOfDiscovery() {
//    return fDateOfDiscovery;
//  }

  /**
  * Returns a mutable object - good style.
  * 
  * Returns a defensive copy of the field.
  * The caller of this method can do anything they want with the
  * returned Date object, without affecting the internals of this
  * class in any way. Why? Because they do not have a reference to 
  * fDate. Rather, they are playing with a second Date that initially has the 
  * same data as fDate.
  */
  public Date getDateOfDiscovery() {
    return new Date(fDateOfDiscovery.getTime());
  }

  // PRIVATE //

  /**
  * Final primitive data is always immutable.
  */
  private final double fMass;

  /**
  * An immutable object field. (String objects never change state.)
  */
  private final String fName;

  /**
  * A mutable object field. In this case, the state of this mutable field
  * is to be changed only by this class. (In other cases, it makes perfect
  * sense to allow the state of a field to be changed outside the native
  * class; this is the case when a field acts as a "pointer" to an object
  * created elsewhere.)
  */
  private final Date fDateOfDiscovery;
}
// This is trivially immutable.
public class Foo {
    private String bar;
    public Foo(String bar) {
        this.bar = bar;
    }
    public String getBar() {
        return bar;
    }
}
public class Womble {
    private final List<String> cabbages;
    public Womble(List<String> cabbages) {
        this.cabbages = cabbages;
    }
    public List<String> getCabbages() {
        return cabbages;
    }
}
// ...
Womble w = new Womble(...);
// This might count as mutation in your design. (Or it might not.)
w.getCabbages().add("cabbage");