File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes singleton creattion! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "singleton creattion!" Watch "singleton creattion!" New topic

singleton creattion!

Mateen Dar
Ranch Hand

Joined: Jan 14, 2005
Posts: 55
hi, how can i create a class to be a singleton. a singleton is a class that can only be used a sinlge time during the execution of a program. am i right. thanks.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Not quite, an instance of a singleton class is unique. There is only one of them in your program. The problems are 1) how to make sure the instance is unique, and 2) how do the various objects in your program know where to find the single unique instance of the singleton class.

You can use the singleton instance as many time as you like, even from multiple threads running simultaneously in your program. (This is perhaps important in solving problem 1.)

So how would you start making your very own singleton?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mateen Dar
Ranch Hand

Joined: Jan 14, 2005
Posts: 55
i didnt understand .. how do u
Grant Gainey
Ranch Hand

Joined: Oct 16, 2005
Posts: 65
Here are some more things to think about:

If you want something to be a singleton, you have to insure that not just anybody can call "new MySingletonClass()" - that could result in multiple copies of my singleton floating around. How can you "hide" the constructor(s) of your Singleton?

Now that you've hidden the constructor - you need to provide a "gatekeeper" method. That gatekeeper needs to be able to construct the singleton, and then return the same reference each time it's invoked. Try google'ing on "Factory Pattern".

Finally, since you want/must be able to invoke your factory without access to a constructor, the Factory method (and the storage for the singleton) need to be Class-level accessible rathe rthan instance-level. Take a look at what declaring something "static" does for you.

Advanced topics (such as, "Mainting Singleton-ness in the face of multiple ClassLoaders" and "Is Singleton really an Anti-Pattern?") left for future discussion.


(Who's learning how to answer "the JavaRanch way" by asking more questions or pointing out areas for investigation. Bear with me while I learn...)

In Theory, there is no difference between theory and practice.<br />In Practice, there is no relationship between theory and practice.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As an exercise, see if you could hide the whole class instead of hiding the constructor. There are a couple ways with different interpretations of just who we want to hide a class from.

Then the question arises, how do you use a class that is completely hidden? Hint: Have you ever used an Iterator for a List? Did you have to know what class it is or how it was created?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Naveen Vooka

Joined: Oct 13, 2004
Posts: 25

Naveen Vooka
DevSquare - Online Application Development
I agree. Here's the link:
subject: singleton creattion!
It's not a secret anymore!