Singleton vs Static Class
- Singleton class has a single instance or object while static class is a bunch of static methods.
- Singleton class can be extended (through an interface) while static class cannot be inherited.
- Singleton methods can be overridden but not of static class.
- Singleton objects are stored in heap but static objects are stored in stack.
- Static method can call another static method simply by its name within the same class.
- Static method can call another non-static methods of same class only through class’ instance/object. Non-static methods can call static methods by Classname.method().
Note: When Java encounters a call to a method that is found within the object it is treated as a call to that object’s method. So isEmpty() in a method is equivalent to this.isEmpty().
Referencing a non-static method or field within a class is only an issue if code is written inside a static method. However, within non static methods, it poses no problem. Since an object is guaranteed to exist when code is ran. So explicitly creating an object is not required.