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.
- Posted by: Anil Patel
- Posted on: August 20 2002 11:42 EDT
Can somebody help me in this improving my understanding.
- Use Singleton class or a Class with Static Methods by sat pat on August 20 2002 15:36 EDT
- Use Singleton class or a Class with Static Methods by Emil Marceta on August 21 2002 00:05 EDT
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.
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.