Two Laptop Bag*
The moose likes Beginning Java and the fly likes class with single instance only Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class with single instance only" Watch "class with single instance only" New topic
Author

class with single instance only

abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

How to write a class in such a way that only ONE instance of it can be created, and anyone who wants to use an instance of the class will always use that one, single instance.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
The key word to use as search criterium is "singleton".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
There is a better way to achieve that than by a singleton class.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7702
    
  20

Campbell Ritchie wrote:There is a better way to achieve that than by a singleton class.

And furthermore, the situations that you need such a class are not as often as you'd think. Have a look at this.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

Can't we proceed like this, create a static counter variable in the class which should behave as singleton and then use that variable to ensure that not more than one instance is created, probably by checking the value of the static variable in the constructor and also increment it each time in the constructor every time the instance is created(constructor will be called each time the instance is created). ??
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7702
    
  20

Ankush Kaundal wrote:Can't we proceed like this, create a static counter variable in the class which should behave as singleton and then use that variable to ensure that not more than one instance is created, probably by checking the value of the static variable in the constructor and also increment it each time in the constructor every time the instance is created(constructor will be called each time the instance is created). ??

You could; however, with Campbell's suggestion you don't have to. My post wasn't telling abrar that he can't do it, just pointing out that for a fully-fledged class, it's not always what you want - even though it might seem so.

Winston
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

thank you all for replying, i will try out your suggestions...
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Campbell Ritchie wrote:There is a better way to achieve that than by a singleton class.



can you please specify how to create one....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
Did you work out anything from the link I posted?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Ankush Kaundal wrote:Can't we proceed like this, create a static counter variable in the class which should behave as singleton and then use that variable to ensure that not more than one instance is created...

I am by no means an expert, but I didn't think this was thread safe without some additional tweaking...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 150

Winston Gutkowski wrote:
And furthermore, the situations that you need such a class are not as often as you'd think. Have a look at this.
Winston


Those examples are too abstract for me. I did some oogling and found this link helpful.
 
 
subject: class with single instance only