How to decide between making a normal class ( with body and methods ) , utlity class ( with static body and static methods ) or a singleton class ?


Class with only static methods can be used if we intend to just call the method and retrieve the output without storing anything, or the storage to be shared among the class accesses. Though we can replicate this behavior using normal classes too, but then Why take the hassle of creating the object when we don't need the body.

Even when we don't need anything to be stored within the class scope and just want to retrieve or post something using methods, Sometime we have no other choice but to work with objects and not with just static members alone and hence need a singleton implementation.

Here are the reasons for having the singleton implementation instead of utility classes -

1. Classes don't participate in Runtime Polymorphism whereas objects are. 
2. Static member elements cannot be serialized whereas object can be.
3. Lazy initialization cannot be performed as static elements are initialized when the class is loaded.

So How to decide which class to be used in which Scenario

1. Identify if the class needs to have a body for its objects with each object needing its own memory space to store values, Go for Normal classes.

2. Identify if the class need not have a body for its objects and can share the class storage, Go for Static class with static methods and static elements.

3. Identify if the class need not have a body for its objects and can share the class storage, but it needs to participate in runtime polymorphism or serialization or needs to be lazily initialized , Go for Singleton Class.