Categories
design-patterns singleton static

Difference between static class and singleton pattern?

1969

What real (i.e. practical) difference exists between a static class and a singleton pattern?

Both can be invoked without instantiation, both provide only one “Instance” and neither of them is thread-safe. Is there any other difference?

7

1375

+50

What makes you say that either a singleton or a static method isn’t thread-safe? Usually both should be implemented to be thread-safe.

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that’s less common, in my experience), so you can pass around the singleton as if it were “just another” implementation.

19

  • 33

    Well, if you prefer it, neither is inherently threadsafe, you have to make them be threadsafe, both of them, so no difference there.

    Feb 6, 2009 at 8:20

  • 127

    Can you give an example of something which is inherently threadsafe, other than immutable types?

    – Jon Skeet

    Feb 6, 2009 at 8:23

  • 26

    To Skeet: People saying that singleton isn’t threadsafe mean that a singleton is shared between threads unnecessarily all the time, while stack objects get shared when you need them to, which means you don’t have to do unneeded synchronization.

    – Iraimbilanja

    Feb 6, 2009 at 10:20

  • 47

    @Geek: Imagine the singleton implements an interface Foo, and you have a method taking a Foo as a parameter. With that setup, callers can choose to use the singleton as the implementation – or they could use a different implementation. The method is decoupled from the singleton. Compare that with the situation where the class just has static methods – every piece of code which wants to call those methods is tightly coupled to the class, because it needs to specify which class contains the static methods.

    – Jon Skeet

    Sep 7, 2013 at 8:01

  • 10

    @AmirBareket: It’s not a singleton according to the singleton design pattern though – if the class itself allows multiple instances to be created, it’s not a singleton IMO, regardless of what the factory does.

    – Jon Skeet

    Apr 10, 2014 at 15:42


520

The true answer is by Jon Skeet, on another forum here.

A singleton allows access to a single
created instance – that instance (or
rather, a reference to that instance)
can be passed as a parameter to other
methods, and treated as a normal
object.

A static class allows only static
methods.

13

  • 90

    Why would you pass a Singleton as a parameter, though, if you can access the same instance from just about anywhere by calling the static getInstance() method?

    Jul 1, 2012 at 9:58

  • 28

    @HenriqueOrdine So it can fit into existing code and provide an interface?

    – user1115652

    Jul 12, 2012 at 10:54

  • 6

    @HenriqueOrdine They are speaking about static class, not a class with static methods. Static class cannot be instantiated. Nevertheless, if you pass an instance of a (non-static)class that contains static methods, you cannot call static methods on an instance.

    – Goran

    Aug 25, 2012 at 0:09

  • 6

    What’s a static class? At least in Java, there’s no such thing.

    Aug 25, 2012 at 9:00

  • 19

    @Goran I was initially very confused by your wording. You said “you cannot call static methods on an instance”. I read that as “if you have a reference to an instantiated object, you cannot call any static methods it may have.” That is of course, incorrect. After reading it again a few times I think you meant “from inside static methods you cannot access non-static objects in the class” which is correct. Want to clarify that for anyone new to these concepts who comes across this answer and reads your comments.

    Apr 30, 2013 at 17:59

393

  1. Singleton objects are stored in Heap, but static objects are stored in stack.
  2. We can clone (if the designer did not disallow it) the singleton object, but we can not clone the static class object
    .
  3. Singleton classes follow the OOP (object oriented principles), static classes do not.
  4. We can implement an interface with a Singleton class, but a class’s static methods (or e.g. a C# static class) cannot.

25

  • 132

    The second statement is wrong. We can’t clone Singleton object. Singleton implementation must refuse this. If you really can clone Singleton, it’s not Singleton.

    Sep 27, 2013 at 7:01

  • 29

    This is answer is not correct for Java: neither the singleton nor the static uses the stack.

    – AgilePro

    Oct 20, 2013 at 18:52

  • 83

    #1 is not important. #2 describes a defective implementation. #3 is completely unjustifiable.

    – Casey

    Mar 5, 2014 at 14:49

  • 37

    How can static object be stored in stack? New stack frame is created when you invoke a method, it stores method’s local variables, this stack frame is removed when the method returns, and those local variables are lost. Sure stack is fast, but it is not suitable to store static objects.

    – mike_m

    Jun 8, 2014 at 10:36

  • 33

    I cannot understand the number of upvotes on this one. 1) Why should Singleton have to be stored in stack? In managed languages like C# or Java data is stored in a managed heap, except for local method variables/parameters. 2) If you can clone it, then it’s not a properly implemented singleton. 3) Singleton is known as a OOP anti-pattern; i.e. something that you should avoid if possible. 4) This is the only thing that’s correct.

    – Groo

    Apr 28, 2016 at 7:05