Sunny Gupta
Sunny Gupta

Reputation: 7067

Making a class singleton without using static method

I need to create a singleton class without keeping a static method.

How can i do that?

Upvotes: 13

Views: 11964

Answers (7)

Adriaan Koster
Adriaan Koster

Reputation: 16209

Another approach is the singleton holder idiom which offers initialization on demand:

public class Something {
        private Something() {
        }
 
        private static class LazyHolder {
                public static final Something INSTANCE = new Something();
        }
 
        public static Something getInstance() {
                return LazyHolder.INSTANCE;
        }
}

Note that standalone singletons like this should be avoided where possible because it promotes global state, leads to hard-to-unittest code and depends on a single classloader context to name a few possible drawbacks.

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533530

Create an enum with one instance

enum Singleton {
    INSTANCE;

    private Field field = VALUE;
    public Value method(Arg arg) { /* some code */ }
}

// You can use
Value v = Singleton.INSTANCE.method(arg);

EDIT: The Java Enum Tutorial shows you how to add fields and methods to an enum.


BTW: Often when you can use a Singleton, you don't really need one as utility class will do the same thing. The even shorter version is just

enum Utility {;
    private static Field field = VALUE;
    public static Value method(Arg arg) { /* some code */ }
}

// You can use
Value v = Utility.method(arg);

Where Singletons are useful is when they implement an interface. This is especially useful for testing when you using Dependency injection. (One of the weakness of using a Singleton or utility class substitution in unit tests)

e.g.

interface TimeService {
    public long currentTimeMS();
}

// used when running the program in production.
enum VanillaTimeService implements TimeService {
    INSTANCE;
    public long currentTimeMS() { return System.currentTimeMS(); }
}

// used in testing.
class FixedTimeService implements TimeService {
    private long currentTimeMS = 0;
    public void currentTimeMS(long currentTimeMS) { this.currentTimeMS = currentTimeMS; }
    public long currentTimeMS() { return currentTimeMS; }
}

As you can see, if your code uses TimeService everywhere, you can inject either the VanillaTimeService.INSTANCE or a new FixedTimeService() where you can control the time externally i.e. your time stamps will be the same every time you run the test.

In short, if you don't need your singleton to implement an interface, all you might need is a utility class.

Upvotes: 22

andrey
andrey

Reputation: 842

public class Singleton {
    public static final Singleton instance = new Singleton();
    private Singleton() {}
    public void foo() {}
}

then use

Singleton.instance.foo();

Upvotes: 9

Eduard
Eduard

Reputation: 3216

From within constructor you need to chceck if there is already instance of the class somewhere. So you need to store reference to your singleton instance in static variable or class. Then in contructor of singleton I would always check if there is existing instance of singleton class. If yes I wouldnt do anything if not I would create it and set reference.

Upvotes: 0

Wint
Wint

Reputation: 2298

Use an object factory, fetch your singleton object from this factory.

ObjectFactory factory;
....
MySingletonObject obj = factory.getInstance(MySingletonObject.class);

of course, there are many frameworks to help you to achieve this. the spring framework is a popular choice.

Upvotes: 0

Artem
Artem

Reputation: 4397

You can use one of IoC containers (e.g. Google Guice) and use your class as singleton(eager or lazy - it depends on your needs). It's easy and flexible as instantiating is controlled by IoC framework - you don't need any code changes if, for example, you will decide to make your class not singleton later.

Upvotes: 1

duffymo
duffymo

Reputation: 308763

Follow the Joshua Bloch enum recipe in "Effective Java" 2nd edition. That's the best way to create a singleton.

I don't understand why this comes up so much. Isn't singleton a discredited design pattern? The GoF would vote it off the island today.

Upvotes: 1

Related Questions