2

I know that static variables are loaded at runtime, and although I thought I understand the issue, I had troubles with the next Eager Initialization Singelton implementation:

In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton class but it has a drawback that instance is created even though client application might not be using it.

public class EagerInitializedSingleton {

private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();

//private constructor to avoid client applications to use constructor
private EagerInitializedSingleton(){}

public static EagerInitializedSingleton getInstance(){
    return instance;
}

}

If the class's constructor is private, how the class instance can be created at the time of class loading? It has only one public entry point, which the client has to call explicitly, right?

Nimrod
  • 1,100
  • 1
  • 11
  • 27
  • 1
    Because it's a static field, and static fields are initialized when the class is loaded, irrespective of whether any instance of the class is created. – Andy Turner Jul 18 '16 at 12:09
  • Side note: A) consider not using singletons B) if you really have to use them, consider using an enum instead. – GhostCat Jul 18 '16 at 12:19

5 Answers5

4

Why in Eager initialization, the instance of the singleton class which created at the time of class loading could consider a drawback?

As the text you quoted says:

"... it has a drawback that [the] instance is created even though client application might not be using it."

Why is that a drawback?

Because:

  • it might be computationally expensive (and increase startup time) to create the instance
  • the instance might take up a lot of memory.

If the class's constructor is private, how the class instance can be created at the time of class loading?

It is created when the initializing expression for instance is evaluated. That happens when the EagerInializedSingleton class is initialized ... which typically happens when the class is loaded.

The expression calls the private constructor. But that is OK because a code in a class can see its own private fields / methods and constructors.

Does it mean the class is getting loaded when the we call the getInstance method?

Not necessarily. It could happen before then. But it won't happen after then1.

1 - ... unless you have created an initialization dependency cycle.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • first, thanks for the title correction - I'll edit it. second - you hae wrote: _It is created when the initializing expression for instance is evaluated. That happens when the EagerInializedSingleton class is initialized_ - so, does it mean the class is getting loaded when the we call the `getInstance` method? – Nimrod Jul 18 '16 at 12:27
1

The static variable is initialized when the class is loaded.

The class is loaded when calling EagarInitializedSingleton.getInstance() but could be loaded earlier if something like EagarInitializedSingleton.class is accessed instead.

fgb
  • 18,439
  • 2
  • 38
  • 52
0

I will just give one very fresh example from my current project. The hibernate repositories to some of the schemas were initialized eagerly as singletons. Also some clients towards external web services. At the same time the subsystem we wanted to work uppon did not need any access to database. Still the application was not startle outside the private network. Also if any of the database servers stop, the application again will not be startable.

If you eagerly load any singletons make sure they dont have any references to external systems at least!

Alexander Petrov
  • 9,204
  • 31
  • 70
0

From java specs

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

T is a class and an instance of T is created.

• T is a class and a static method declared by T is invoked.

• A static field declared by T is assigned.

• A static field declared by T is used and the field is not a constant variable (§4.12.4).

In your case

T is a class and an instance of T is created.

Above condition triggers the initialization of class

Jabir
  • 2,776
  • 1
  • 22
  • 31
0

In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton class but it has a drawback that instance is created even though client application might not be using it.

The Singleton Class is created at the time of class loading irrespective of Client use this class or not. It's not a drawback really.

If the class's constructor is private, how the class instance can be created at the time of class loading? It has only one public entry point, which the client has to call explicitly, right?

Yes. Constructor is private to the class. It's not visible to any other class. But public methods in same class can call private constructor. In your example, below line calls private constructor.

private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();

This line is executed even before first invocation of

public static EagerInitializedSingleton getInstance()

If you need efficient lazy Singleton, refer to below SE question:

Why is volatile used in this example of double checked locking

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211