File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Singleton in java" Watch "Singleton in java" New topic

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

Joined: Aug 03, 2008
Posts: 9453

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: 15041

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 8 API documentation
I agree. Here's the link:
subject: Singleton in java
It's not a secret anymore!