• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is singleton

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i am new to this forum and this is my first message in this catogory , i want to know what is singleton means and how to implement and the scenario where it is necessary to implement. pls, if anyone knows about this pls reply .

thank you.
Mohan dodderi
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of singleton class is to enforce atmost only one instance of class is created.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say a Huge application has a utility like Logger or a Database Connection Pool managing Utility.

These kinda utilities cannot exist in multiple instances for a application, so usually for such cases you can go for Singleton Classes.

There are lots of discussion in web which discusses about Static Vs Singleton Classes, you can look for them to know more.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this article from sun helps you.
 
mohan dodderi
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much Srinivasa Raghavan, Senthil.B.Kumar.

regards
Mohan dodderi
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also http://faq.javaranch.com/view?SingletonPattern
 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton is OK for some situations, such as when you are running a standalone application. You are only guaranteed to get one instance per classloader.

For other situations (such as web application servers, J2EE, etc.) this pattern breaks down horribly. You would have to use some other paradigm (JNDI registry, etc.) to ensure you have only one instance of something.

Singleton has other problems, such as the fact that you can never extend/inherit from a Singleton. (Some would argue this is a feature.)

Also, you have be careful if your Singleton is serializable, because the JVM may create a second instance of your class when it is deserialized. The book "Effective Java" by Joshua Bloch has a great explanation of that issue.

Also, make sure your factory method is synchronized. Otherwise 2 threads may create 2 instances in the factory method.

Geoffrey
[ March 09, 2005: Message edited by: Geoffrey Falk ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The biggest problem with Singletons actually is the global access throught the static instance() method - it makes them being "global variables in disguise", and therefore come with the same problems as global variables: heavy coupling.
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic