• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what are singleton classes ?

 
sunanda kadam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain it
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not to forget about a static factory method, often named getInstance(), which acecesses the private constructor on demand.
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The better version of singleton template is:

 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just slightly better:

Mind you, singleton objects usually should not be Serializable, and most definitely not Cloneable.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/SingletonPattern
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic