• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

singleton vs static

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see Mohamed Sanaulla has also alluded to your confusion between "singleton or static".
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Nice demonstration of builders, and a nested class.
 
Grow your own food... or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic