Share on Google+Share on Google+

Java Singleton Pattern

In this section, you will learn about singleton pattern in Java.

Java Singleton Pattern

In this section, you will learn about singleton pattern in Java.

What is Singleton Pattern ?

In Java, singleton pattern is implemented to limit the instantiation of a class to one object. Similar to static field, instance of a class will occur only once per application.

How to implement ?

You can implement the singleton pattern as given below :

public class SingletonDemo {
// Private constructor restrict from instantiating
private static SingletonDemo instance = new SingletonDemo();

private SingletonDemo() {

public static SingletonDemo getInstance() {
return instance;

// This methods is protected due to singleton
protected static void sampleMethod() {
System.out.println("Singleton Sample Method");

For implementing "singleton pattern",  you need a private constructor( SingletonDemo() ) and a field to conatin its result(instance), and a static access method having name like getInstance().

public class SingletonExample {
public static void main(String args[]){




Posted on: December 6, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Java Singleton Pattern  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image