• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Allow to create only ONE instance of a class

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

This is the Brain Barbell from HFJ, Page#280.Chapter#10, Numbers and Statics.

What if you want 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?

Someone please enlighten me how to achieve this.

Thank you.

Until next time,

Chaitanya
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek & welcome to the forum.

Originally posted by Vivek Chaitanya:
Hi All

This is the Brain Barbell from HFJ, Page#280.Chapter#10, Numbers and Statics.

What if you want 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?

Someone please enlighten me how to achieve this.

Thank you.

Until next time,

Chaitanya



The Math() class is in this category. Take a look at it to get some ideas. Also, I read this, but forget if it was HFJ or K&B, *think* (and look for topics on) private constructors.

Since this is a brain buster, I'm not going to explicitly tell .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If you really want to have one and only one instance of that class you need to do just two things.

1. Make the constructor(s) of that class private.
2. Now to create an instance of that class you need to have a public static method inj that class. Also take a reffernce variable of the same class.
This method say getInstance() will check the above said reffernce variable and if found null, call the private constructor and assign the object to the reffernce variable. this method then return the instance. If it find the reffernce variable to be not null Then it just return the old object.

i hope it will help you.

-- Shekhar
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chander shekhar:
This method say getInstance() will check the above said reffernce variable and if found null, call the private constructor and assign the object to the reffernce variable. this method then return the instance. If it find the reffernce variable to be not null Then it just return the old object.


Note that this does not guarantee that only a single instance is created in a multi-threaded environment (i.e., it is not thread-safe).
 
chander shekhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes You are right.
I missed out that thing. In that case we need to synchronize the method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic