Meaningless Drivel is fun!*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 865
    
    5
The key word to use as search criterium is "singleton".
Campbell Ritchie
Sheriff

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

Joined: Mar 17, 2011
Posts: 7081
    
  16

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?
Artlicles 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: 7081
    
  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, 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: 36599
    
  16
Did you work out anything from the link I posted?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

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: 140

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: class with single instance only
 
Similar Threads
about Singleton Pattern
confuse about singleton
Allow to create only ONE instance of a class
Information about Singleton
Servlet container will create only one instance of the class?