aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Allow to create only ONE instance of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Allow to create only ONE instance of a class" Watch "Allow to create only ONE instance of a class" New topic
Author

Allow to create only ONE instance of a class

Vivek Chaitanya
Ranch Hand

Joined: Oct 19, 2007
Posts: 35
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
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
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
chander shekhar
Greenhorn

Joined: Sep 18, 2007
Posts: 2
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
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).


Ping & DNS - my free Android networking tools app
chander shekhar
Greenhorn

Joined: Sep 18, 2007
Posts: 2
Yes You are right.
I missed out that thing. In that case we need to synchronize the method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Allow to create only ONE instance of a class