Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between static class and singleton pattern?

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?

like image 287
Jorge Córdoba Avatar asked Feb 06 '09 08:02

Jorge Córdoba


People also ask

What is difference between singleton and static class in Java?

Singleton is a design pattern that assures a single instance of a Class for the lifetime of an application. It also provides a global point of access to that instance. static – a reserved keyword – is a modifier that makes instance variables as class variables.

What is the difference between singleton and static method and when to use?

Singleton pattern uses Heap memory. Static classes use stack memory. It works within the scope of Garbage Collector as it uses Heap memory.

Why can't we use static class instead of singleton?

While a static class is generally initialized when it is first loaded and it will lead to potential class loader issues. Singleton Objects stored on heap while static class stored in stack. Singleton Objects can have constructor while Static Class cannot.

When we have static class Why do we need singleton design pattern?

The advantage of using a static class is the compiler makes sure that no instance methods are accidentally added. The compiler guarantees that instances of the class cannot be created. A singleton class has a private constructor that prevents the class from being instantiated.

What is the difference between Singleton and static class?

Singleton objects are stored in Heap, but static objects are stored in stack. We can clone (if the designer did not disallow it) the singleton object, but we can not clone the static class object . Singleton classes follow the OOP (object oriented principles), static classes do not.

What is the difference between a singleton and variation?

A singleton is a class with just one instance, enforced. That class may have state (yes I know static variables hold state), not all of the member variables or methods need be static. A variation would be a small pool of these objects, which would be impossible if all of the methods were static.

What are the advantages of the singleton pattern?

The Singleton pattern has several advantages over static classes. First, a singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members).

What is singleton class in Java?

Singleton instance is created for the first time when the user requested. Singleton class can have constructor. You can create the object of singleton class and pass it to method. Singleton class does not say any restriction of Inheritance. We can dispose the objects of a singleton class but not of static class.


1 Answers

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.

like image 64
Jon Skeet Avatar answered Oct 09 '22 08:10

Jon Skeet