This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
deepak Wrote: carter below is small code for singleton pattern.
The constructor is private and if single object is created, this is fine. If you use multiple threads you need to protect the getInstance() through synchronization other wise it might return two different instances of Singleton object
Its not thread safe, multiple threads might compete for getInstance() and 2 threads might end up with totally different instances. One approach to solve this is by using double checked locking, but that has its own share of concerns like the Instance being available to a different thread even before the initialising thread has completed the initialisation i.e initializing the state.
In the book Effective Java this concept has been clearly explained.