Your code is not demonstrating properly. This is due to a double idiom, where it makes sense:
// Double-check idiom for lazy initialization of instance fields. private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null) // Second check (with locking) field = result = computeFieldValue(); } } return result; }
Read about it here .
Be careful that this idiom is a good choice only for instance fields. In your question, you have a static field, for which the simplest idiom is the main choice: the lazy identifier of the owner class:
// Lazy initialization holder class idiom for static fields private static class FieldHolder { static final FieldType field = computeFieldValue(); } static FieldType getField() { return FieldHolder.field; }
source share