This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes what are singleton classes ? 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 » Java » Beginning Java
Bookmark "what are singleton classes ?" Watch "what are singleton classes ?" New topic
Author

what are singleton classes ?

sunanda kadam
Ranch Hand

Joined: Feb 26, 2008
Posts: 42
Please explain it
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by sunanda kadam:
Please explain it


Singleton is a Design pattern for allowing only one instance of your class.
(i.e.,) You are allowed to create only one Object for your class.
Laxmikant Ruikar
Greenhorn

Joined: Nov 29, 2005
Posts: 23
Singleton is a design pattern to create a single(or some numbers) instance of class by keeping constructor of class as private.
BABUN GHOSh
Greenhorn

Joined: Apr 03, 2008
Posts: 7
according to set theory when a set contain only one element is called a singletone set. example A={5}

so single tone class is a class which can able to create only single instance of that class.


Aveek
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
not to forget about a static factory method, often named getInstance(), which acecesses the private constructor on demand.
Vilmantas Baranauskas
Ranch Hand

Joined: Dec 20, 2006
Posts: 89
The better version of singleton template is:



Author of <a href="http://www.newsinjector.com" target="_blank" rel="nofollow">NewsInjector</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

And just slightly better:

Mind you, singleton objects usually should not be Serializable, and most definitely not Cloneable.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Then you might just as well make the singleton instance public static final, which will save you the getInstance() method alltogether ...

One good reason for initializing it on demand is resource issues: Don't create the object unless you need it. At least for large singletons.
[ May 03, 2008: Message edited by: Guido Sautter ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Then it's ok to not have the field be final, but then you shouldn't initialize it upon declaring. If you initialize a singleton when you declare it, it's most logical to make it final.

But in your code it is not wise to make it final
It does suffer from one possible problem: if multiple threads access the method at the same time, it is possible to create two instances. You'll need some form of synchronization, the simplest being to just synchronize the entire method.
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
Quote

--------------------------------------------------------------------------
And just slightly better:

code:
--------------------------------------------------------------------------------

private static final SingletonExample instance = new SingletonExample();


--------------------------------------------------------------------------------

---------------------------------------------------------------------------

above code can be considered when you have multithreade enviornment.
you need to add some synchronization check in your getinstance method ,which will prevent race condition.
lazy creation of object gives you better performance when you do not require the object frequently.There it gives performance benefit.


Examples of singleton , Runtime class in java is singleton,Printer,there are other several examples of singleton.

________________________
Mintoo
SCJP 1.4
____________________________
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I have never encountered a case where lazy instantiation made sense, so I would always instantiate the class as in Rob's example. This is thread safe and requires no synchronization of any methods.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://faq.javaranch.com/java/SingletonPattern


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what are singleton classes ?