I read a lot of articles about Singleton, in most of which authors said that this variation of Singleton in Java:
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return instance;
}
}
is NOT LAZY (EAGER then).
But I can't understand why, Singleton()
constuctor will be invoked only on Singleton
class initialization. I know several reasons, which can trigger class initialization:
- Using
new
with constructor (but in this case constructor is private). - Accessing or setting up static field (here private).
- Using static method.
- With reflection:
Class.forName("Singleton")
.
So here our object will be created only on using static method getInstance()
(it is still LAZY initialization I guess) and with reflection (but reflection can ruin a lot of Singleton variations, except enum
maybe).
Maybe I can't see something obvious, explain me please, where was I wrong?
Basically it's a matter of degrees of laziness. It's lazy in that it won't construct the singleton until the class is initialized, but it's eager in that there could be situations where you want to use the class without initializing the singleton itself.
For example:
Calling
Singleton.sayHello()
will instantiate the singleton even if we don't want it to... so it's not as lazy as it could be.You can get round this using a nested type:
Now
Singleton.Holder
will only be initialized by using thegetInstance
method. It's lazy and thread-safe with no locking.In my experience, usually a singleton class's only static method is the
getInstance
method, in which case they're equivalent (assuming you don't use reflection to initialize the type somehow, for example).It is not lazy because the singeton object is created once the class is loaded.
A lazy Singleton would create the object when it is first used.
In my point of view, the goal of being Lazy is to control when the instance of the Singleton is created. In this example, there is no control because there is a class variable (static member that is initialized when the class is initialized and not when getInstance() method is called).
That way, there is no control of when the Singleton is created.
But, if you initialize the variable, inside getInstance, you are getting control of when you want the Singleton to be created.
As you said, there are many examples of Singletons. I always try to go for Enums if I need them. You can find examples here: https://connected2know.com/programming/java-singleton-pattern/