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
-
Use Singleton class or a Class with Static Methods (2 messages)
- Posted by: Anil Patel
- Posted on: August 20 2002 11:42 EDT
Threaded Messages (2)
- 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
-
Use Singleton class or a Class with Static Methods[ Go to top ]
- Posted by: sat pat
- Posted on: August 20 2002 15:36 EDT
- in response to Anil Patel
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. -
Use Singleton class or a Class with Static Methods[ Go to top ]
- Posted by: Emil Marceta
- Posted on: August 21 2002 00:05 EDT
- in response to sat pat
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