Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 1
    Fair enough! I'm just comfortable with volatile and it's use. Oh, and three cheers for JCiP. Commented Sep 16, 2008 at 14:40
  • 1
    Oh, this is apparently the approach advocated by William Pugh, of FindBugz fame. Commented Sep 16, 2008 at 14:42
  • 4
    @Stu The first edition of Effective Java (copyright 2001) details this pattern under item 48. Commented Mar 6, 2010 at 1:31
  • 10
    @Bno: What about making constructor private? Commented Mar 18, 2013 at 18:16
  • 2
    @AlikElzin-kilaka Not quite. The instance is created in the class loading phase for BarHolder, which is delayed until the first time it's needed. Bar's constructor can be as complicated as you want, but it won't get called until the first getBar(). (And if getBar is called "too early" then you'll face the same problem no matter how singleons are implemented.) You can see the lazy class loading of the code above here: pastebin.com/iq2eayiR Commented Sep 9, 2015 at 15:53