• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

class with single instance only

 
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1043
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key word to use as search criterium is "singleton".
 
Marshal
Posts: 74053
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a better way to achieve that than by a singleton class.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all for replying, i will try out your suggestions...
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74053
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you work out anything from the link I posted?
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic