Consider the Singleton :
public final class MySingleton {
private static class Nested
{
private static final MySingleton INSTANCE = new MySingleton();
}
private MySingleton()
{
if (Nested.INSTANCE != null)
{
throw new IllegalStateException("Already instantiated");
}
}
public static MySingleton getInstance()
{
return Nested.INSTANCE;
}
}
I didn't put any locks , but why is this a thread safe solution for the Singleton problem ?