Singleton Design Pattern says that there should be only one instance of the class at any time during the running state of the application in which it exists. It means that there should only one instance of the class in the JVM.

If you run the below code it will always return you the same instance every time you call the Singleton.getInstance(). Now will you say that “what is the fun in this? why should I need to have a same instance all the time?”.

Well the answer is to save memory. Instead of creating new instances we can resue the same instance whenever required. But doing so we have to remember that state of the singleton object does not change with upon its usage. That means you cannot just blindly start creating singleton objects for their corresponding classes. Remember object represents a real world entity. That means you and I cannot be a singleton object. Most popular example of a Singleton object is Logger object or a Security Manager object.

As you can see in the code that I have declared the constructor as private, well the purpose is to stop others creating new instances of this class. I am sure you must be aware of that. If you dont never mind, now you know it.

Singleton class maintains a static reference to the only singleton instance created during the first call to Singleton.getInstance() and returns a reference to that instance. Check out that the getInstance method is a static method.

 Java |  copy code |? 
01
package com.test.singleton;
02
 
03
public class Singleton { 
04
 
05
private static Singleton object = null; 
06
 
07
/** 
08
  * Private Constructor.
09
  *
10
  */ 
11
  private Singleton() {
12
  } 
13
 
14
  /**
15
   * This method always returns the only instance of the class Singleton. 
16
  * @return 
17
  */ 
18
   public static Singleton getInstance() { 
19
     if (object == null) { 
20
       synchronized (Singleton.class) {
21
         if (object == null) { 
22
           object = new Singleton(); 
23
           return object; 
24
         } 
25
       } 
26
     } 
27
     return object;
28
  }
29
 
30
}

I have used a class level lock in the synchronized block as instance level lock cannot be acquired in the static context. That means you cannot use "this" reference in a static method. But why I have used a lock?

Let us consider that there are two active threads T1 and T2 and both tries to get the instance of the Singleton class and Singleton.getInstance() has not yet been called by anyone. If T1 is preempted by T2 at Line Number 19 before the assignment is made, the member variable will still be null during the time, and T2 can subsequently enter the if block and we will land up with two distinct singleton instances because T1 is already inside the if block. So we had used a lock at Line Number 20 to make the code thread safe and checked again whether the object is still null nor not.

Also see "How serialization breaks Singleton".
Let us consider that there are two active threads T1 and T2. If T1 is preempted by T2 at Line Number 2 before the assignment is made, the member variable will still be null during the time, and T2 can subsequently enter the if block we will land up with two distinct singleton instances because T1 is already inside the if block. So we need to use lock to make the code thread safe.Let us consider that there are two active threads T1 and T2. If T1 is preempted by T2 at Line Number 2 before the assignment is made, the member variable will still be null during the time, and T2 can subsequently enter the if block we will land up with two distinct singleton instances because T1 is already inside the if block. So we need to use lock to make the code thread safe.
Sharable with
  • Digg
  • Facebook
  • Google Bookmarks
  • Diigo
  • MySpace
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Reddit
  • Twitter
  • DZone
  • email
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • blogmarks
  • MSN Reporter
  • PDF