Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why private static field = new Singleton is not lazy in Java?

Tags:

java

singleton

I read a lot of articles about Singleton, in most of which authors said that this variation of Singleton in Java:

public class Singleton {
    private static Singleton instance = new Singleton();

    private Singleton() {}
    public static Singleton getInstance() {
        return instance;
    }
}

is NOT LAZY (EAGER then).

But I can't understand why, Singleton() constuctor will be invoked only on Singleton class initialization. I know several reasons, which can trigger class initialization:

  1. Using new with constructor (but in this case constructor is private).
  2. Accessing or setting up static field (here private).
  3. Using static method.
  4. With reflection: Class.forName("Singleton").

So here our object will be created only on using static method getInstance() (it is still LAZY initialization I guess) and with reflection (but reflection can ruin a lot of Singleton variations, except enum maybe).

Maybe I can't see something obvious, explain me please, where was I wrong?

like image 469
marshall Avatar asked Jun 30 '14 09:06

marshall


People also ask

What if I use static instead making singleton?

While a static class is generally initialized when it is first loaded and it will lead to potential class loader issues. Singleton Objects stored on heap while static class stored in stack. Singleton Objects can have constructor while Static Class cannot.

What is Singleton design pattern why it is used for Why not static class?

Singleton is a design pattern that makes sure that your application creates only one instance of the class anytime. It is highly efficient and very graceful. Singletons have a static property that you must access to get the object reference.

Does singleton allow lazy initialization?

Lazy initialization is possible. It is also thread safe.

Should singleton fields be static?

The static instance variable inside of a singleton is meant to hold the only instance of the class that will exist. It is static because it will need to be referenced by a static method 'GetInstance()' that will return the instance, or will create the instance if it is the first time that 'GetInstance()' was called.


2 Answers

Basically it's a matter of degrees of laziness. It's lazy in that it won't construct the singleton until the class is initialized, but it's eager in that there could be situations where you want to use the class without initializing the singleton itself.

For example:

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

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }

    public static void sayHello() {
        System.out.println("Hello!");
    }
}

Calling Singleton.sayHello() will instantiate the singleton even if we don't want it to... so it's not as lazy as it could be.

You can get round this using a nested type:

public final class Singleton {
    private Singleton() {}

    public static Singleton getInstance() {
        return Holder.instance;
    }

    public static void sayHello() {
        System.out.println("Hello!");
    }

    private static class Holder {
        private static final Singleton instance = new Singleton();
    }
}

Now Singleton.Holder will only be initialized by using the getInstance method. It's lazy and thread-safe with no locking.

In my experience, usually a singleton class's only static method is the getInstance method, in which case they're equivalent (assuming you don't use reflection to initialize the type somehow, for example).

like image 129
Jon Skeet Avatar answered Oct 16 '22 22:10

Jon Skeet


It is not lazy because the singeton object is created once the class is loaded.
A lazy Singleton would create the object when it is first used.

like image 21
AlexWien Avatar answered Oct 17 '22 00:10

AlexWien