This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes singleton vs static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "singleton vs static" Watch "singleton vs static" New topic
Author

singleton vs static

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 557
    
    7

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

Joined: Sep 05, 2008
Posts: 21
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.



Manish Doomra
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
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..


SCJP 6
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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?


Mohamed Sanaulla | My Blog
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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

Joined: Oct 13, 2005
Posts: 37953
    
  22
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

Joined: Oct 13, 2005
Posts: 37953
    
  22
I see Mohamed Sanaulla has also alluded to your confusion between "singleton or static".
suraj aryan
Greenhorn

Joined: Nov 19, 2010
Posts: 14
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

Joined: Oct 13, 2005
Posts: 37953
    
  22
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

Joined: Dec 15, 2013
Posts: 5

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.


Nothing endures but change. - Heraclitus (c. 535 – c. 475 BCE)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Welcome to the Ranch

Nice demonstration of builders, and a nested class.
 
wood burning stoves
 
subject: singleton vs static
 
Similar Threads
Singleton Question
Singleton vs Static class
benefits of HashMap() over Hashtable
Singleton Objects
Static vs Single Pattern