Singleton vs Static class

Singleton vs Static

Singleton vs Static

The difference between singleton class vs static classes is one of the standard questions which you may encounter in your interviews.
. There are many articles around this topic, I found many differences between these two. So the obvious question that arose what is common between these two. The answer is, there is only one instance is created for Singleton and Static class. On the other hand, the Static class doesn’t have the concept of instances so even this is ruled out. The answer is nothing is common between these two. Do let me know in the comments if they have some common behavior. Some of the differences are.

SingletonStatic
Singleton is a design pattern, in which only one instance of a class is created.Static is a keyword when a class is defined as static it cannot be initiated and provide other properties.
You can Inherit, derive a Singleton class. i.e
Singleton classes follow the OOP (object-oriented principles)
We cannot derive or inherit from a singleton class. Static class doesn’t follow OOP.
Singleton objects are stored in Heap. Static objects are stored in stack.
Singleton class has a constructor. Static Class cannot have a constructor.
A Singleton can be initialized lazily, that is when the class is called for the first time. Static class is initialized when it is first loaded.
Singleton class can have static and non-static methods.Static class can have only static methods
Singleton classes are unit testable, we can mock them easily.Mocking a static class is a challenge.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *