Singleton Pattern

Singleton Pattern is a creational pattern, one of the simplest design patterns in Java. This pattern involves a single class which is responsible for creating an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

In the following example SingleObject class is created. SingleObject class has its constructor as private and also has a static instance of itself. SingleObject class provides a static method to get its static instance to outside world. Demo class SingletonPatternDemo will use SingleObject class to get SingleObject object.

Singleton Pattern UML Diagram

Step 1 : Create Singleton Class

public class SingleObject {
private static SingleObject instance = new SingleObject();

private SingleObject() {}

public static SingleObject getInstance() {
return instance;

public void showMessage() {
System.out.println("SingleObject object method is invoked");

Step 2 : Get the only object from the singleton class

public class SingletonPatternDemo {
public static void main(String[] args) {
SingleObject singleObject = SingleObject.getInstance();
The output will be :

SingleObject object method is invoked

Leave a Reply

Your email address will not be published. Required fields are marked *