Difference between Singleton and a static class

In simple words, singleton is a pattern and not a keywork vs. static is keyword for .Net language. You can create singleton classes any modern day object oriented language. Use static class in c# code and you have singleton code ready for your work. Depending on how you initialized object it becomes thread safe or unsafe. --- Saurin TravadiApr 06, 2011

this is a wrong answer.
you mean to say static class and singleton class has no difference.
Then why on earth singleton exists if there is already static class and both are same according to you. --- Sajjad AhmadApr 17, 2012

Singleton = Single means single object across the application life cycle & the scope of singleton is at application level.
Static= Static an not have any object pointer & the scope of static is at App Domain level.

I hope this will make your doubt clear.

--
Kaushal
http://kaushalp.blogspot.com

1 comment

Hello Kaushal
Thanks for this, but tell me one thing which I can do from the singleton that I can do same thing from the static class. Then what is the use of a static class? --- Anshuman SainiAug 31, 2010

Hello.. In addition to responses already provides here are my two cents why would you use a Singleton in favor of static class.

As you know both by now Singleton and Static classes both provide a central point of access i.e. there's only one instance and one way to access it.

The benefit of Singleton class is it is much more flexible that static classes. Static classes once defined could not accomodate any future design changes as by design static classes are rigid and cannot be extended.

Singleton on the other hand provides flexibility and also provides sort of a mechanism to control object creation based on various requirements. They can be extended as well if need arises. In other words you are not always tied to a particular implementation. With Singleton you have the flexibility to make changes as when situation demands..

Hope this add some more clarity to the thoughts about its usage.

1 comment

Not at all satisfied by the answer.
Need some concrete answer which says this is this possible / benefits of singleton which cannot be done by simple static class --- Sajjad AhmadApr 17, 2012

There is no functional difference between the following:
int x = SomeClass.doSomething(1,2);
int y = AnotherClass.GetInstance().doSomething(1,2);

Personally, I feel the second construct (what you call "singleton") is more flexible because it hides object management and subclassing from the application layer. For example, I can make the following changes rather easily:

// Converts from singleton to factory
public static AnotherClass GetInstance() {
return new AnotherClass(); // factory method, returns a new object instance each time
}

Both are very significant changes but we can effect them WITHOUT changing the code.
int y = AnotherClass.GetInstance().doSomething(1,2); // this line of code still works whether you use singletons, factory methods, or abstract factory methods

If you wanted to accomplish the same thing using static methods, you would definitely have to change the code
SomeClass s = new SomeClass(); // explicitly declare a new instance
int x = s.doSomething(1,2);

or
int x = SomeOtherClass.doSomething(1,2); // add another class altogether if you want keep the same method signature and multiply instead of add

2 comments

Here's a simple rule for usage: If your design calls for inheritance and variation then you must use a Singleton and not a Static class as you cannot use inheritance with static classes, by definition. In most usages, you would want your Singleton to implement an interface, something you cannot do with a static class. --- Saleh NajarApr 11, 2011

The difference between the Singleton and Static is Singleton Class can have value when Class object instantiated between server and client, such a way if three client want to have a shared data between them Singleton can be used. Static are always just shared and have no instance but multiple references.

Thank you,

Rajesh

1 comment

Thats a good point and one possible answer.
I think thats why singleton class can be used for state mangement in stateless scenarios like shopping cart scenario, --- Sajjad AhmadApr 17, 2012