Why can’t we use a static class instead of singleton?

Intention of a singleton pattern is to ensure that a single instance of a class is instantiated. So why not static class?
Reasons are below :-

- One of the key advantage of singleton over static class is that it can implement interfaces and extend classes while the static class cannot (it can extend classes, but it does not inherit their instance members). If we consider a static class it can only be a nested static class as top level class cannot be a static class. Static means that it belongs to a class it is in and not to any instance. So it cannot be a top level class.

- Static class will have all its member as static only unlike Singleton.

- It can be lazily loaded whereas static will be initialized whenever it is first loaded.

- Singleton object stores in Heap but, static object stores in stack.

- We can clone the object of Singleton but, we can not clone the static class object.

- Singleton can use the Object Oriented feature of polymorphism but static class cannot.

 

Can the singleton class be subclassed?

Answer: Singleton is just a design pattern and it can be subclassed. However it is worth to understand the logic or requirement behind subclassing a singleton class as the child class might not inherit the singleton pattern objective by extending the Singleton class. However the subclassing can be prevented by using the final keyword in the class declaration.

Protected Constructor:

It is possible to use a protected constructor to in order to permit the subclassing of the singeton. This techique has 2 drawbacks that makes singleton inheritance impractical:

- First of all, if the constructor is protected, it means that the class can be instantiated by calling the constructor from another class in the same package. A possible solution to avoid it is to create a separate package for the singleton.

- Second of all, in order to use the derived class all the getInstance calls should be changed in the existing code from Singleton.getInstance() to NewSingleton.getInstance().

5 Thoughts on “Why can’t we use a static class instead of singleton?

  1. verishal on July 28, 2015 at 12:22 pm said:

    Nice …………explain

  2. Dip Ghosh on July 12, 2021 at 12:46 am said:

    I didnt understand.. ->the static class cannot (it can extend classes, but it does not inherit their instance members)
    Can you give an example?

  3. I believe it can can extend the values of a base non static class

    or else can you please give an example of->while the static class cannot (it can extend classes, but it does not inherit their instance members).

  4. import java.io.*;

    class A
    {
    String second = “A”;
    }

    class C{
    static class B extends A
    {

    String first = “B”;
    B()
    {
    super();
    }

    }
    public static void main(String args[]){
    B obj = new B();
    System.out.println(obj.first);
    System.out.println(obj.second);
    }
    }

    ============output
    B
    A
    ============

    I believe it can can extend the values of a base non static class

    or else can you please give an example of->while the static class cannot (it can extend classes, but it does not inherit their instance members).

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation