Java Design Patterns .

Java Design Patterns Singleton

Singleton Overview

A class distributes the only instance of itself.

In this example SingleSpoon class holds one instance of SingleSpoon in "private static SingleSpoon theSpoon;". The SingleSpoon class determines the spoons availability using "private static boolean theSpoonIsAvailable = true;" The first time SingleSpoon.getTheSpoon() is called it creates an instance of a SingleSpoon. The SingleSpoon can not be distributed again until it is returned with SingleSpoon.returnTheSpoon().

If you were to create a spoon "pool" you would have the same basic logic as shown, however multiple spoons would be distributed. The variable theSpoon would hold an array or collection of spoons. The variable theSpoonIsAvaialable would become a counter of the number of available spoons.

Please also note that this example is not thread safe. I think that to make it thread safe all you would need is to make the getTheSpoon() method synchronized. Many thanks to Brian K. for pointing this out. - a Singleton

public class SingleSpoon 
   private Soup soupLastUsedWith;
   private static SingleSpoon theSpoon;
   private static boolean theSpoonIsAvailable = true;
   private SingleSpoon() {}
   public static SingleSpoon getTheSpoon() {
       if (theSpoonIsAvailable) {
           if (theSpoon == null) {
               theSpoon = new SingleSpoon();
           theSpoonIsAvailable = false;
           return theSpoon;
       } else {
           return null;
           //spoon not available, 
           //  could throw error or return null (as shown)
   public String toString() {
       return "Behold the glorious single spoon!";
   public static void returnTheSpoon() {
       theSpoonIsAvailable = true;
   public Soup getSoupLastUsedWith() {
       return this.soupLastUsedWith;
   public void setSoupLastUsedWith(Soup soup) {
       this.soupLastUsedWith = soup;

public void cleanSpoon() { this.setSoupLastUsedWith(null); } }
To download source right-click here and "Save As...". - testing the singleton

class TestSingleSpoon {
   public static void main(String[] args) {
       System.out.println("First person getting the spoon");
       SingleSpoon spoonForFirstPerson = 
       if (spoonForFirstPerson != null)
           System.out.println("No spoon was available");
       System.out.println("Second person getting a spoon");
       SingleSpoon spoonForSecondPerson = SingleSpoon.getTheSpoon();
       if (spoonForSecondPerson != null)
           System.out.println("No spoon was available");
       System.out.println("First person returning the spoon");
       spoonForFirstPerson = null;
       System.out.println("The spoon was returned");
       System.out.println("Second person getting a spoon");
       spoonForSecondPerson = SingleSpoon.getTheSpoon();
       if (spoonForSecondPerson != null)
           System.out.println("No spoon was available");
To download source right-click here and "Save As...".

Test Results

First person getting the spoon
Behold the glorious single spoon!

Second person getting a spoon No spoon was available
First person returning the spoon The spoon was returned
Second person getting a spoon Behold the glorious single spoon!


UML for Singleton


Portland Pattern Repository
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Java Design Patterns - A Tutorial by James W. Cooper
Comments Comments are left by visitors to and may or may not be accurate.
Comment by david_david on 2008-02-20 Rate this Comment

If we are using the singleton class like specified in "Comment by gamlidekgamlidek"
Suppose MySingleton class have one more public method say
public String processData(String s){//code here};

I think the above method should be make synchronized like
public synchronized String processData(String s){//code here};

otherwise if we access this method from different threads will make problem.. right???

Comment by archive on 2015-11-02 Rate this Comment

I realy appreciate the Java Patterns on your site. Since I like to play with the examples I simply copied it into text fils and compiled them. I found a few minor problems you may want to know about:

- class SingleSpoon declares a private Soup but the class is not defined anywhere.

Comment by Rath N on 2008-06-26 Rate this Comment

public class TestString {

SingleSpoon singleSpoon = null;

public static void main(String[] args) {


public TestString getInstance() {
if (singleSpoon == null) {
synchronized (this) {
if (singleSpoon == null) {
singleSpoon = new SingleSpoon();
return singleSpoon;

Comment by gamlidek on 2010-12-11 Rate this Comment

A good implementation of a simple pure singleton in Java needs to account for the clone method, as mentioned by archive above, and shouldn't hold a lock on the object like you're doing. Also, note that two getInstance() calls can occur at the same time possible causing two instances to get created, so I made the getInstance() method synchronized. BTW, if you have areas that require synchronization, then put it in a syncronized block.

public class MySingleton {
/* (non-Javadoc)
* This is a reference to the one and only instance of
* this singleton object.
private static MySingleton ref;

* This is the only way to access the singleton instance.
* @return A reference to the singleton.
public static synchronized MySingleton getInstance()
if (ref == null)
ref = new MySingleton();
return ref;

/* (non-Javadoc)
* Since this is a singleton object, we must disallow cloning.
* @see java.lang.Object#clone()
public Object clone() throws CloneNotSupportedException
throw new CloneNotSupportedException();

/* (non-Javadoc)
* This is the only constructor, and since this is a singleton instance,
* the constructor is private and can only be called from the static
* instance accessor method.
* @see #getInstance()
private MySingleton() {


Comment by ram25 on 2006-10-02 Rate this Comment

There still needs some correction. We need to synchronize the check to null in the getTheSpoon() method. Simpler yet make the function synchronized. This is because, call A to the function could complete the check and be stalled by the OS scheduler, and so with call B. Now that in that situation there are two function calls that think the instance variable is still null and eventually instantiate the instance variable leading to a race condition.

Comment by archive on 2006-04-27 Rate this Comment

Just found your site. It's a great resource so has gone straight into my

One nit pick I had was the Java Singleton example. It doesn't specify the clone() method so I could do:

SingleSpoon spoonOne = new SingleSpoon();
SingleSpoon spoonTwo = spooneOne.clone();

Hurah for my 2 spoons.

Anyway, it's simply fixed by implementing your own clone method with an UnsupportedOperationException thrown or something else to stop the spoon stealers!

Sign in to comment on Java Design Patterns Singleton.