I thought that all static fields initialized in the order in which they appear in the source. However, then I saw the following code:
public static class Singleton {
private static class InstanceHolder {
public static Singleton instance = new Singleton(); //LINE X
}
private Singleton(){}
public static Singleton getInstance() {
return InstanceHolder.instance;
}
}
Does it mean that field on LINE X
is initialzed when getInstance
method is called first time? Could anyone explain?