I have a class whose fields can't help but lazily initialized.
class Some {
public Some getPrevious() {
{
final Some result = previous;
if (result != null) {
return result;
}
}
synchornized (this) {
if (previous == null) {
previous = computePrevious();
}
return previous;
}
}
// all fields are final initialized in constructor
private final String name;
// this is a lazy initialized self-type value.
private volatile Some previous;
}
Now sonarcloud keep complaining with java:S3077
.
Use a thread-safe type; adding "volatile" is not enough to make this field thread-safe.
- Is there anything wrong with the code?
- Can(Should) I ignore it?
- What about using
AtomicReference
? Isn't it an overkill?