I've been studying for a job interview and I've found this question:
What's wrong with the following Singleton factory method getInstance()?
public class Singleton {
private static Singleton mySingleton;
protected Singleton() {}
public static Singleton getInstance() {
if (mySingleton == null) {
synchronized(Singleton.class) {
if(mySingleton == null) {
mySingleton = new Singleton();
}
}
}
return mySingleton
}
}
I know the constructor is wrong and should be private. However, they're asking about what's wrong with the getInstance() method, everything looks fine to me, I've seen a lot of examples like this one (in a multithreading environement) what am I missing?