6

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?

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47

1 Answers1

2

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.

dotvav
  • 2,808
  • 15
  • 31
  • As dotvav states, main difference is that a Singleton is an object instance os a class, thus you can work with it as an Object, is to say you can reference it, serialize, pass as argument, it can implement interfaces, extends clases, ... Static class has no this advantages. – malaguna Sep 10 '15 at 09:40
  • I actually want to know difference in performance factor in java. – Gaurav Jeswani Sep 10 '15 at 16:31
  • Then you should ask a new question and don't forget to ask about performance. – dotvav Sep 10 '15 at 16:40