When need a singleton, is a static field a elegant solution?
class HelperSingleton {
static Helper singleton = new Helper();
public static Helper getInstance() {
return singleton;
}
}
When two threads access to getInstance
at the same time, is there a chance the field singleton
is not initialized completely? Or see the default values for fields of the helper object, rather than the values set in the constructor?
Static singleton is also lazy initialization?
I mean,
static Helper singleton = new Helper();
is this assigment atomic? And won't return default values ?