File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 9280
    
  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: 13875
    
  10

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
 
 
subject: Singleton in java
 
Similar Threads
singleton class
Creating a utility managed bean, to move common code and use from different beans?
Single pattern
Tech Word Game
Hashtable's put method problem in locking