This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Singleton in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Singleton in java" Watch "Singleton in java" New topic
Author

Singleton in java

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Can anyone please explain me the meaning of Singleton with example ...


Thanks<br />Dinesh
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well singleton is a pattern in which you can create only one instance of a class. You implement it by making the constructor private and creating a static factory method which sends the only object of the class to the caller.

This is a sample code-



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

See Singleton pattern in Wikipedia and Java Singleton Design Pattern.

Note that some people regard the singleton design pattern as an anti-pattern (a bad thing). See, for example Why Singletons Are Controversial.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Singleton in java
 
Similar Threads
Tech Word Game
Creating a utility managed bean, to move common code and use from different beans?
Single pattern
Hashtable's put method problem in locking
singleton class