I am trying to fix double checked locking with Bloch's effective java recommendation. But a small variation in my code is that the field type is static and the method to create the field type is instance method. Will the below variation for creating a static field work?
private static volatile FieldType field4;
// NOTE: The code for this method in the first printing had a serious error (see errata for details)!
public FieldType getField4() {
FieldType result = field4;
if (result != null) // First check (no locking)
return result;
synchronized(Initialization.class) {
if (field4 == null) // Second check (with locking)
field4 = computeFieldValue();
return field4;
}
}
I cannot make the method static method to // Lazy initialization holder class idiom for static fields - Page 334.