Stupid Java Stuff: static initializers and singletons

So, to get it out of the way: there is a fairly large segment of the Java community whose reaction to a Java-style singleton object is to hold their nose and go fleeing in the opposite direction, pausing only to git blame to know who to come back for and settle accounts with later. I know this because we all have Singleton Haters Club cards and the meeting is at the corner bar every Tuesday evening. But this post is largely in spite of that.

The topic of singleton initialization came up today. There are a lot of bad ways to initialize a singleton and I got to see a couple of them up close and personal today; that there are ostensibly principal-level Java developers who haven’t internalized Effective Java is a surprise but nobody’s perfect. Anyway, without further ado, lemme give you the wrongest (vaguely working) way to do it that I can think of.

read more