lundi 17 avril 2017

Java: Singleton implementation- enum vs static final variable

What's the different between two ways to implement singleton? Which one is better?

As far as I know, using enum or using static final variable will both lead to lazy loading that when the enum(or class in static final variable way) is referenced at the first time, the enum(class) will be initialized, so the singleton object will be initialized too. However, many say that the static final variable way ia not lazy loading, because you may find a way to initialize the class when you are not referencing the singleton object, which is the same when it comes to using enum to implement singleton. So why using enum is considered lazy loading while using static final variable is not?

Aucun commentaire:

Enregistrer un commentaire