We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hey! Actually the code you posted is an example of eager initialization[1], and not lazy initialization- you'd want to avoid this since if the initialization cost is high, and if you end up not using the Singleton, it would be a waste of resources.
As for why the SingletonHolder, it's the use of an idiomatic design pattern, which Wikipedia can explain in more detail if you'd like to learn more[2].
Java Singleton Pattern
You are viewing a single comment's thread. Return to all comments →
Hey! Actually the code you posted is an example of eager initialization[1], and not lazy initialization- you'd want to avoid this since if the initialization cost is high, and if you end up not using the Singleton, it would be a waste of resources.
As for why the SingletonHolder, it's the use of an idiomatic design pattern, which Wikipedia can explain in more detail if you'd like to learn more[2].
[1] http://cs.nyu.edu/courses/fall16/CSCI-UA.0470-001/slides/lecture25.pdf#page=52
[2] https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom