Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton vs static

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have seen several postings in this forum but I did not get clarity on the differences between singleton and static method behavior in a class. I feel that whatever we can achieve using static method we can achieve with Singleton. If you can provide sample code, It would be really great. Let us make this thread as One Stop Shop for Singleton.
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to start off with, please provide your understanding of what static is/means and that singleton is/means and we can use that as a starting point.

 
Manish Doomra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding for Singleton is:-
We are restricted to create only single instance of the class.

The following code depicts the framework of Singleton class:-



while static member has a class level access, whereas in case of singleton the instance is merely static and restricted to get created only once.

Following code depicts the usage of static:-



Both produce the same output, but the method calling is different in both the cases.

Steve Fahlbusch wrote:Well to start off with, please provide your understanding of what static is/means and that singleton is/means and we can use that as a starting point.

 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well one difference is that an outer class can be singleton but an outer class can not be static.
That is, you can not make a singleton class by using static..
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:Hi, I have seen several postings in this forum but I did not get clarity on the differences between singleton and static method behavior in a class. I feel that whatever we can achieve using static method we can achieve with Singleton. If you can provide sample code, It would be really great. Let us make this thread as One Stop Shop for Singleton.


Static methods are not part of the instance. But singleton is an object. The restriction there is that only one object of a particular type can exist. I dont see why you are trying to compare Static methods with Singleton- which boils down to comparing methods and objects?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me change the question, In what case(s), I should go for Singleton class instead of static class(I mean, static methods in a class) and in what cases I can for static classes instead of Singleton.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two are completely different. You can find discussion of static in "beginning Java™", and a singleton is a design pattern whereby a certain number of objects can be created from a class. That "certain number" is usually 1; in fact the word "singleton" implies 1, but there are analogous patterns which allow exactly 2 instances for example.

You should not go on about "singleton or static". That is a bit like going on about "bicycles or cheese" A question without an answer.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see Mohamed Sanaulla has also alluded to your confusion between "singleton or static".
 
suraj august
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should go for Static class if you want to have some utilty class whereas Singelton comes when a class can have state and the states can changes (but still object is one ).
The singleton class would be useful if it contained a set of variables that we wanted only one set of and the methods used those variables but in our helper class we don't use any variables apart from the ones passed in (which we make final).
static classes implementation cannot be mocked but you can mock the implementation of singelton using Factory class.
A static class is one that has only static methods, for which a better word would be "functions". The design style embodied in a static class is purely procedural.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not called a static class. There is nothing static about it. It is an ordinary top‑level class which has only static members. As you say, it is often called a utility class.
 
Barna Biro
Greenhorn
Posts: 5
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static classes are quite useful in certain situations, builders are one such example:

Admittedly, the above is quite abstract, but I hope it's enough to get the idea... Singletons on the other hand are usually used for manager-like classes where only one instance has to exist at any given point in time. As others have already pointed out, defining only static methods in a class, doesn't make it a "static class", it's just a class having all members defined as static ( usually, utility classes fit this profile ).

When to use which? Well, it all comes down to what you need to solve...
The two things are not the same, they help solve quite different problems altogether.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Nice demonstration of builders, and a nested class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic