aspose file tools
The moose likes Java in General and the fly likes singleton vs static Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply 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
Ranch Hand

Joined: Sep 18, 2000
Posts: 496
    
    2

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: 101
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
Bartender

Joined: Sep 08, 2007
Posts: 2946
    
  15

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: 32827
    
    4
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: 32827
    
    4
I see Mohamed Sanaulla has also alluded to your confusion between "singleton or static".
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: singleton vs static
 
Similar Threads
Static vs Single Pattern
Singleton Objects
benefits of HashMap() over Hashtable
Singleton vs Static class
Singleton Question