Over a million developers have joined DZone.

Singleton Pattern - Explained

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Singleton Pattern is a pattern that ensures that there is only ever one single instance of a class. And it provides a global way to get to that instance. This class will prevent any other class to create more than one instance of the class.

First step is to create a Java class with a private constructor. Now only code that lives in the singleton class will be able to call this private constructor. Then we have to add a private static variable unique, whose type is Singleton. Please see the code below.

public class Singleton {
	private static Singleton uniqueInstance;
 
	private Singleton() {}
 
	public static Singleton getInstance() {
		if (uniqueInstance == null) {
			uniqueInstance = new Singleton();
		}
		return uniqueInstance;
	}
}

The second times we call getInstance, if that instance already exists, then we just return one which we already made, ensuring that there's only one instance. But this is not thread safe.

There are two ways to make this class singleton.

  • Create static instance at the start of the class and always return that instance from the getInstance method. The JVM will create the instance before any threadaccesses the static variable.  Here the issue is we always create an instance,we're creatingit eagerly rather than lazily, even before invoking the getInstance method. 

public class Singleton {
	private static Singleton uniqueInstance = new Singleton();
 
	private Singleton() {}
 
	public static Singleton getInstance() {
		return uniqueInstance;
	}
	
	public String getDescription() {
		return "Singleton class";
	}
}

  • Second way is to make the getInstance class synchronized. This solution creates the Singleton lazily, like the classic Singleton does, so the object is created only if you really need it. Synchronization can cause poor performance; synchronized code takes a lot longer to run than unsynchronized code. And because get instance method is synchronized, every call to get instance will be more expensive.

public class Singleton {
	private static Singleton uniqueInstance;

 
	private Singleton() {}
 
	public static synchronized Singleton getInstance() {
		if (uniqueInstance == null) {
			uniqueInstance = new Singleton();
		}
		return uniqueInstance;
	}
 
	
	public String getDescription() {
		return "Singleton class";
	}
}

Which way to create your singleton depends on your application? If you need lazily initialization, then go with synchronized. Otherwise create singleton static instance at the start of the class.


Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}