Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

wat is singletone  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai!

Wat is singletone pattern, can anybody help me with an one example,how to make class as a singletone
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Singleton Pattern, You can't create a object for others class. They are restricted by making the constructor of their as private.


For the Singleton class the following is Mandatory:
1. Constructor should be private.
2. The object should be static.
(i.e., static classname ref = new classname(); )
3. Use static method to get the instance.


May be this link will be useful http://www.coderanch.com/t/400320/java/java/Singleton
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Mandatory" is a bit strong there. Singleton has a mechanism to control the number of instances of a class. The number is usually "one" but there could be cases where some other small, controlled number is appropriate. And there are other mechanisms to control creation besides a private constructor.

The simplest mechanism is as described above:


Do look up the other threads on this topic. We've discussed WHY you'd want a singleton and some of the dangers many times before.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you ever heard of Google? If not, check out all the links it gives that describe the Singleton pattern.

Layne
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!