在Singleton的这种基本实现中,new Singleton1()调用实际上何时发生(标记为*

public final class Singleton1 {

    private Singleton1() { }

    private static Singleton1 instance = new Singleton1(); // (*) When does this happen?
                                                           //     Before calling getInstance() from the client?

    public static Singleton1 getInstance() {
        return instance;
    }
}

最佳答案

当JVM首次加载该类时,将填充静态字段(请参见JLS 12.4)。因此,可以在调用instance之前填充getInstance()

09-26 15:51