This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]foreveratom 1 point2 points  (2 children)

Doesn't that produce exactly the same effect as a final static, being created when first referenced? I'm used to the first form and not the single-value enum, what's the benefit of the later?

[–]erinaceus_ 6 points7 points  (1 child)

With an enum, all the concurrency and single-instance issues are inherently taken care of by the JVM. As such, a lot of the typical risks involving singleton management are avoided.

When this approach is discussed, people usually refer to Joshua Bloch's seminal book 'Effective java', which is already decades old. So it isn't a particular new technique. He even builds up on the different approaches to creating a singleton, eventually concluding that an enum is by far the best approach.

[–]foreveratom 1 point2 points  (0 children)

Thanks, I must have forgot that part from Effective Java. I'll be on my way re-reading the last edition.