Consider a third-party class like
class A {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
which I make immutable like
final class ImmutableA extends A {
public ImmutableA(int value) {
super.setValue(value);
}
public void setValue(int value) {
throw new UnsupportedOperationException();
}
}
The visibility guarantee for final fields doesn’t apply here. My question is if other threads are guaranteed to see the correct state of ImmutableA.
If not, is there a solution? Using delegation is not an option since I need ImmutableA to be an A.
You could use
synchronized, though this may slow down the getter: