I came across the below class in the book "Java concurrency in practice". The author states that the state of the Holder can be stale in the below example resulting in AssertionError when assertSanity is called. How can the below Holder class be made Immutable to overcome this?
public class Holder{
private int n;
public Holder(int n) { this.n = n;}
public void assertSanity() {
if(n != n)
throw new AssertionError(" this statement is false");
}
}
//unsafe publication
public Holder holder;
public void initialize(){
holder = new Holder(42);
}