File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton implementation Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton implementation Question" Watch "Singleton implementation Question" New topic

Singleton implementation Question

Ranch Hand

Joined: Apr 26, 2005
Posts: 54

typically a singleton implementation is like:

public final class Singleton {
private static Singleton single = new Singleton();

private Singleton() {}

public static Singleton getInstance() {
return single;

public void doSomethingMethod() {
// do something here

Another implementation could be:

public abstact class Singleton {

public static void doSomethingMethod() {
// do something here

I'm looking for the reasons why the first implementation is better than the second implementation?


manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
well the second code is no Singleton.

1)it is abstract -> no instantiation possible
2)even if it was not abstract, there could be more than one instance in your JVM, so it couldn't be a singleton.

aldana software engineering blog & .more
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
The first implementation - the actual Singleton pattern - is more flexible:

- the getInstance method could return an instance of a subclass, and

- you can pass the object around, so that not all clients need to know it's a singleton.

And there is an even more flexible pattern - the Just Create One pattern.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Could anybody explain why we required singleton class at all???(some live project ex. expected).

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

Could anybody explain why we required singleton class at all???

Marking your constructor private will prevent your clients from instantiating you class directly. If suppose they want to access your class instance methods they need an instance of your class. So how do they get an instance of your class?

If you look at the first example provided Susan Oso, you can understand why Singleton is needed?

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Kamesh Loganathan

Joined: Jan 21, 2006
Posts: 20
Generally we use the singleton design pattern for factory classes, manager classes and classes for which we want only once instance in the whole application. It generally comes in very handy in Java swing programming.
I agree. Here's the link:
subject: Singleton implementation Question
It's not a secret anymore!