Your example works fine, so it is not complete.
Perhaps in your real application you have a dependecy loop between your classes, so getSingleton()
is called before Someclass
initialization is Someclass
, something like the following, but with a few classes involved:
public class Foo { private static Foo INSTANCE = new Foo();
This is especially likely if you have several interdependent singleons implemented this way. Try to find and eliminate these cycles.
Also, it might be better to use some kind of DI or Service Locator pattern instead of manually executing singleton behavior.
axtavt
source share