The Big Moose Saloon Question and Answer of the Month

Mosey on in and pull up a stool. The JavaRanch Big Moose Saloon is the place to get your Java questions answered. Our bartenders keep the peace, and folks are pretty friendly anyways, so don't be shy!

We pulled one out from the deep past for this inaugural column. Over in The Java In General (Beginner) Forum, JavaRanch's own Frank Carver did a heck of a job tellin' folks about making a Singleton in Java. Following is an excerpt from the original forum thread.

Question: What's a Singleton?

Answer: A singleton is an object of which there may only ever be a single instance. (*)

You can make your own class act like a singleton, simply by only creating a single instance. But this relies on every programmer who uses your code knowing that there is only supposed to be one instance of the class, which is dangerous. A better solution is to write the class in a way which prevents a casual programmer from creating more than one instance. This is where the Singleton pattern and its associated Java idioms help you.

Lets start with a simple class:

public class Something
{
  private String content = "empty";
  
  public void setContent(String content)
  {
    this.contemt = content;
  }
  
  public String getContent()
  {
    return content;
  }
}

The most obvious way for a programmer to try to create an instance of a class is to call its constructor, so we need to prevent this. If we just fail to provide a constructor, Java assumes that there is a "default constructor" taking no arguments which just creates an instance of the class; not what we need! The Java "trick" for this is to have a constructor, but make it private so no other classes may call it.

public class Something
{
  private String content;
  
  private Something()
  {
    content = "empty";
  }
  
  public void setContent(String content)
  {
    this.contemt = content;
  }
  
  public String getContent()
  {
    return content;
  }
}

Now that we have no constructor, we need to make a way for people to create a single instance of the class. Typically this is done by provding a static method which creates an instance, but will create only one. If it is called again, it just returns the same instance that it created the first time.

public class Something
{
  private static Something instance = null;

  private String content;
  
  private Something()
  {
    content = "empty";
  }
  
  public static Something getInstance()
  {
    if (instance == null)
    {
      instance = new Something();
    }
  
    return instance;
  }
  
  public void setContent(String content)
  {
    this.contemt = content;
  }
  
  public String getContent()
  {
    return content;
  }
}

Now the code is almost complete. The last thing to remember is that Java is a multi-threaded language in which many things can be happening "at once". If two threads were to call getInstance at the same time, then our class might occasionally create two instances, which would be wrong and very hard to test. So we make use of Java's built-in synchronization mechanism to make sure that only one thread can call getInstance at any one time.

So the final singleton class looks like:

public class Something
{
  private static Something instance = null;

  private String content;
  
  private Something()
  {
    content = "empty";
  }
  
  public synchronized static Something getInstance()
  {
    if (instance == null)
    {
      instance = new Something();
    }
  
    return instance;
  }
  
  public void setContent(String content)
  {
    this.contemt = content;
  }
  
  public String getContent()
  {
    return content;
  }
}

I hope this helps.

(*) The Singleton pattern actually allows for a specified number of instances, but singletons are harly ever used for any number other than one.