11

Possible Duplicate:
Difference between static class and singleton pattern?

Just wanted to know what exactly is the difference between a singleton and static class?

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578

4 Answers4

7

In a singleton you can choose to initialize the variable at first call. Whereas a static variable starts to exists at the moment you include / call the file where the static variable is declared.

Anemoia
  • 7,928
  • 7
  • 46
  • 71
  • And if the file is autoloaded? Presumably then the static class doesn't come to exist until you first call it, like with a singleton? – Codemonkey Jun 14 '20 at 13:33
1

Singleton is the pattern that has nothing to do with the implementation in a particular language. Technically singleton says that you can have only one instance of object in the system. In case of static class you will not have any objects at all.

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
Juriy
  • 5,009
  • 8
  • 37
  • 52
1

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces.But static wont

Difference between static class and singleton pattern?

Community
  • 1
  • 1
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
1

A singleton is an abstract design pattern that describes an object that can only be created once. A static class is a specific implementation of this design pattern.

Cerin
  • 60,957
  • 96
  • 316
  • 522