Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class with single instance only

 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key word to use as search criterium is "singleton".
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a better way to achieve that than by a singleton class.
 
Winston Gutkowski
Bartender
Pie
Posts: 10218
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10218
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all for replying, i will try out your suggestions...
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you work out anything from the link I posted?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic