Want to write articles/new answer to be published in javapedia.net? Send in your article with your name to javatutorials2016@gmail.com!

Prev Next

Java / Design Patterns

How to implement a singleton pattern?

Create a public class having a private constructor. Private constructor ensures that the object cannot be created outside of the class.

Create a private static variable of the type Class itself.

Create a public static method e.g. getInstance() that returns the reference of the static variable, if the static variable is null then create a object for the class.

package org.javatutorials.designpatterns;

//Step 1
public class SingletonPattern {

	//Step 2
	private static SingletonPattern mySingletonObj;

	private SingletonPattern() {


	public static SingletonPattern getInstance() {

		if (mySingletonObj == null) {
			mySingletonObj = new SingletonPattern();


		return mySingletonObj;

Invest now!!! Get Free equity stock!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.

More Related questions...

What is design pattern in Java? What is Observer design pattern in Java? Name few design patterns used in standard JDK library in Java. Explain strategy design pattern in Java. Difference between Strategy and State design Pattern in Java. Singleton pattern. How to implement a singleton pattern? Why do we need Singleton class? (or) what are the advantages of using singleton pattern? What is Decorator pattern in Java? Give an example of Decorator pattern in Java. Difference between Decorator and Proxy pattern in Java. When to use Setter and Constructor Injection in Dependency Injection pattern? Explain Composite design Pattern in Java. Explain Front controller design pattern in Java/J2ee. Explain MVC design pattern in J2EE. How do you implement a thread-safe Singleton in Java? What is the difference between factory and abstract factory pattern in Java? Is Decorator an EJB design pattern in Java? Explain Facade design pattern. Types of Java Design patterns. Name few Creational Patterns in Java. Name some of the structural design patterns in Java. Name some of the Behavioral design patterns in Java. Explain Builder Design Pattern. Explain Factory pattern in Java. Difference between Service Provider Interface (SPI) and Application Programming Interface (API). Explain Service Provider framework pattern. Explain telescopic constructor pattern in Java. Explain Java beans pattern. Disadvantages of Java beans pattern. Explain builder pattern in Java. Disadvantage of Builder pattern. When to use Builder design pattern? In Java singleton, how do you prevent creating instance using reflection? How do we ensure Singleton Design Pattern in a clustered Environment? Difference between Singleton Pattern and static Class in Java. Give example of singleton class from JDK. When to use Static Class in place of Singleton in Java? Explain Bridge design pattern in Java. Explain prototype pattern in Java. Explain Template method design Pattern in Java. What is filter pattern in Java? Explain Proxy design pattern in Java. What is Adapter pattern in Java? Difference between the Bridge and Adapter pattern in Java. What is Chain of Responsibility Pattern in Java? Explain Intercepting Filter pattern in Java. Disadvantage of using factory pattern for object creation. Which pattern provides greater abstraction than factory pattern? Disadvantages of the singleton pattern. Which classes are candidates of Singleton? What is double checked locking in Singleton? How to prevent creating another instance of Singleton using clone() method? How to prevent creating another instance of Singleton during serialization? Explain mediator pattern in Java. Give few examples of Mediator Pattern in JDK. Explain Command design pattern in Java. What is State Design Pattern in Java? When cloning an object is preferred over creating a new object in Java? Name some of the design patterns that makes use of singleton pattern. Explain Memento pattern in Java. When do you use memento design pattern ? Explain visitor pattern in Java. What is value object design pattern? What is Transfer object design pattern in Java? Explain Service Locator design pattern in Java. Mention some of the core J2EE design patterns. Difference between Front Controller and View Helper in J2EE. Advantages of Factory method design pattern. What is a monolithic architecture? Is template method pattern an example of Inversion of control? Give an example of Template method pattern from JDK. Disadvantages of Immutable class. Types of messaging patterns. Difference between filter and interceptor patterns in Spring MVC.
Show more question and Answers...


Comments & Discussions