I know there are lot of questions on Singleton pattern. But here what I would like to know about the output which might also cover how "static" works in Java.
public class Singleton {
private static Singleton currentSingleton = new Singleton();
public static Singleton getSingleton() {
return currentSingleton;
}
private Singleton() {
System.out.println("Singleton private constructor...");
}
public static void main(String[] args) {
System.out.println("Main method...");
}
}
This is the output from running the code...
Singleton private constructor...
Main method...
When I debugged this code, control went first to line
System.out.println("Singleton private constructor...")
and prints.
(private static variable currentSingleton is still null at this point)
Then it goes to line
private static Singleton currentSingleton = new Singleton();
and then initializes the private variable.
Then at last, it goes to main() method and prints.
My Questions are:
- Why it first prints "Singleton private constructor..." which is in private constructor.
I thought control should first go to main() method since it is the entry point. Also I am not creating any instance anywhere (except in variable initialization).
- Later it goes to static variable instantiation line (currentSingleton=null at this point)
private static Singleton currentSingleton = new Singleton();
Though currentSingleton gets a value here, why constructor is not called again?
Mainly I want to know the flow of control of this program.
You cannot invoke the main method in the class until it has been properly initialized (i.e. static fields and static blocks have been evaluated). When it is initialized, an instance of your singleton is created by invoking the private constructor. Later the main method is invoked.
The class in question has a static field to which you assing a value. Since the field is static it must be initialized before the class can be used in any context, that is, it must receive a value. In this case its value happens to be an instance of the same class. This is what triggers your private costructor during class initialization.
If you want to delve into the process and understand it better please refer to the Java Laguage Specification. More specifically in the section12.4 Initialization of Classes and Interfaces you will find further details.
whenever control comes to any class for the first time, all the static initializations take place first. That's why your static object is being instantiated before anything else by calling it's constructor.
Class gets loaded and initialized first by classloader of JVM. And JVM while initializing, scans the class (Singleton) and while doing that it initializes static variable which is in the first line. That variable calls the constructor and prints line in it. Once class is initialized, it will invokes main method and hence prints the statement inside it.
Before the control gets to main() method, the class needs to be initialized. Since you initialize the currentSingleton
inline with declaration, this initialization takes place before main(), during class loading.
You should rather declare it as final
private static final Singleton currentSingleton = new Singleton();
- final class variables and fields of interfaces whose values are compile-time constants are initialized
Answer to your both questions From JLS#8.3.2. Initialization of Fields
If the declarator is for a class variable (that is, a static field), then the variable initializer is evaluated and the assignment performed exactly once, when the class is initialized (§12.4.2).
The new Singleton()
statement is the first to be executed because the currentSingleton
static field must be initialized; this means that memory for the Singleton object being created is allocated and its constructor is executed prior to assign the resulting object to the field variable.
This is the reason of the System.out.println("Singleton private constructor...");
line being executed before the assignment.
Moreover static field initialization occur as soon as the class is referenced and calling the main function of the Singleton class means refer it and this is the reason the initialization is executed before the main method.