Hope you might find the below code snippet useful:
Note that you have to synchronize an object
public class Singleton
{
private static Singleton m_Singleton = null;
public static int i_Vallue=0;
private Singleton()
{
}
public Singleton getInstance()
{
synchronized(m_Singleton)
{
if(m_Singleton == null)
{
m_Singleton = new Singleton();
}
}
return m_Singleton;
}
public static void showValue()
{
System.out.println(i_Vallue++);
}
public static void main(
String[] args)
{
new Singleton().showValue();
new Singleton().showValue();
new Singleton().showValue();
Singleton.showValue();
}
}
[ November 27, 2003: Message edited by: Vijaykumar Dafal ]