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?
Asked
Active
Viewed 5,453 times
6
-
There can be only one! :) – OldCurmudgeon Sep 10 '15 at 09:30
-
A good summary of advantages / disadvantages of Singleton versus Static : http://javarevisited.blogspot.fr/2013/03/difference-between-singleton-pattern-vs-static-class-java.html – Michael Zilbermann Sep 10 '15 at 09:33
-
refer to https://en.wikipedia.org/wiki/Singleton_pattern – Ravindra babu Sep 10 '15 at 09:37
-
@OldCurmudgeon LOL! the Singleton pattern, a.k.a. 'the Highlander' – francesco foresti Sep 10 '15 at 09:46
-
I want to know the difference in performance of these two in java. – Gaurav Jeswani Sep 10 '15 at 16:32
1 Answers
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