This is the example:
http://www.tutorialspoint.com/java/java_using_singleton.htm
When looking at the 1st example, when user writes : Singleton.getInstance() , then it calls out :
new Singleton()
I don’t get it, how it is singleton, when everytime it creates a new singleton object?
I understand the 2nd example. If singleton is null, then create new object, but in first example, it always creates new object??
Whats up with that?
No, in the first example the only call to
new Singleton()is here (withinSingleton):That’s a static variable initializer. It gets executed once, and only when needed. (If you never touch the
Singletonclass, the initializer won’t get executed.)