Three ways of implementing singleton [Effective Java]

28 09 2013

Singletons is a well known pattern. No matter whether you like or dislike it, it happens to be useful. Joshua Bloch in “Effective Java” reminds about three methods to implement it. One of them is not obvious, but quite interesting (Singleton as Enum)

1. Member as a public final field

public class Elvis {
	public static final Elvis INSTANCE = new Elvis();

	private Elvis() { ... }

	public void leaveTheBuilding() { ... }

Here the private constructor is called only once to initiate public static final field. Once it is initialized it reminds unchanged. But best practice tells to use accessor methods:

2. Singleton with static factory

This is probably the most popular implementation of singleton pattern:

public class Elvis {
	private static final Elvis INSTANCE = new Elvis();
	private Elvis() { ... }
	public static Elvis getInstance(){ return INSTANCE; }
	public void leaveTheBuilding() { ... }

Here also the private constructor is called only once, and getInstance() method is an accessor to private static final field and returns always the same reference.

The advantage here over first case is that such accessor clearly indicates that this is a singleton, not a public constant

3. Enum Singleton

This is the most interesting way to implement singleton – simply create Enum type:

public enum Elvis {

	public void leaveTheBuilding() { ... }

This approach is available since Java 1.5 (when Enums where introduced). It is not widely used, however it is the best way to implement singleton, according to the author, because it:

– is concise

– provides serialization for free

– gives 100% guarantee that INSTANCE will never have another instantiation (even in case of sophisticated serialization and reflection attacks)

I would only recommend to name this class ElvisSingleton instead of Elvis just to clearly indicate that this is singleton.

Effective Java

Effective Java (Joshua Bloch) is a book with advanced Java programming techniques that make Java code more robust and readable. I am currently reading it and this is why these [Effective Java] posts come up here. These are summaries of chapters that are most valuable in my opinion.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!



2 responses

24 07 2015

why there is donation?

16 10 2015
Java: Using Enums for Singletons | Karl Brodowsky's IT-Blog

[…] Three ways of implementing singleton […]

Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: