Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is advantage of Singleton class? [duplicate]

In java when we create a class singleton it create only one instance of that class per application, but rather than creating singleton class if we create all the methods and variables of that class static then also it will create only one instance of that static members. Then what is advantage of singleton class over all static members of class?

like image 461
Gaurav Jeswani Avatar asked Sep 10 '15 09:09

Gaurav Jeswani


1 Answers

This is not only about java. One difference between singletons and static members is that you may have several singletons that extend or implement a same class or interface. If you need to call a method on one of these singletons, you can rely on polymorphism, which would not be the case with static members.

like image 103
dotvav Avatar answered Oct 06 '22 02:10

dotvav