File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Doubt regarding singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Doubt regarding singleton" Watch "Doubt regarding singleton" New topic
Author

Doubt regarding singleton

sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
Hi all,

if i had a singleton class say x and two class a and b trying to get the instance of x at the same time, what will happen. Say a get the instance then will the object of x be shared in between a and b?
Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148
Yes, if you design a thread-safe Singleton.
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
So you mean to say that a single copy of instance will be shared between all the instantiating class objects. Right?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
So you mean to say that a single copy of instance will be shared between all the instantiating class objects. Right?

I think you may be getting confused about what a Singleton is and how it works.

When some code asks for a n instance of a Singleton it does not actually "instantiate" it. It just asks for one, usually by calling a static method somewhere. If an instance has already been created, that's the one it gets. Client code should never call "new" on a Singleton class, that's one of the key things which make it a Singleton.

Depending how the Singleton class is coded, the instance may be created when the Singleton's class is loaded into the JVM, or the instance may be created as a side effect the first time some client code asks for an instance. In practice, that doesn't really matter.

As a side note, please be aware that the Singleton design pattern is hugely over-used, and (in my opinion, and that of many people here) is almost never a good idea. Can you tell us any more about what you might be thinking of using a Singleton for?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
So you mean to say that a single copy of instance will be shared between all the ...other... objects. Right?


I could buy this except that "shared" is not a well defined word here. I would say that many clients could reference the same Singleton. Just as many clients could reference the same non-singleton. Nothing new there.


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
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
Originally posted by Frank Carver:
Can you tell us any more about what you might be thinking of using a Singleton for?


Thank you so much, i think i got my doubts cleared.
 
jQuery in Action, 2nd edition
 
subject: Doubt regarding singleton
 
Similar Threads
singleton synchronized doubt
Singleton Class
Multiple Singleton instances.
Singleton
Atomic (Theadsafe) operations in Java