Discussions

EJB programming & troubleshooting: Use Singleton class or a Class with Static Methods

  1. I have used and written lot of Singleton classes. But I am not clear about the advantages or Usefullness of Singleton class over defining a Class with Static Methods on it.
    Can somebody help me in this improving my understanding.
    Anil Patel
  2. All the methods in Singleton can be overridden and can be used for inheritance etc. Where as static methods cannot be overwritten. Depends on the usage also. I prefer singleton for this reason and prefer initializing object in the beginning only i.e like private static XXXXClass xxx = new XXXClass() over constructing object in getInstance with two null checks.
  3. The double-checked locking should be avoided as it is actually broken.

    The safe approach for lazy instantiation is with inner static singleton holder class.

    See http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

    for more.

    /e