关于内联来自 effective_java 本书的调用

about inline the call from effective_java book

我对 effective_java 这本书有疑问, 'modern Java virtual machine (JVM) implementations are almost certain to inline the call to the static factory method.' 是什么意思 我不明白 'inline the call to the static factory method'

引用:

The main advantage of the public field approach is that the declarations make it clear that the class is a singleton: the public static field is final, so it will always contain the same object reference. There is no longer any performance advantage to the public field approach: modern Java virtual machine (JVM) implementations are almost certain to inline the call to the static factory method

"public field approach"是(摘自书本"Effective Java" By Josh Bloch):

// Singleton with public final field
public class Elvis {
    public static final Elvis Elvis = new Elvis();
    private Elvis() { ... }

    public void leaveTheBuilding() { ... }
}

虽然您在引用中提到的方法是静态工厂:

// Singleton with static factory
public class Elvis {
    private static final Elvis INSTANCE = new Elvis();
    private Elvis() { ... }
    public static Elvis getInstance() { return INSTANCE; }

    public void leaveTheBuilding() { ... }
}

你提到的引用解释了静态工厂方法的 "performance penalty"(因为我们正在调用方法 getInstance 而不是直接通过以下方式使用字段:Elvis.INSTANCE)不再存在(或者它存在的可能性很小),因为编译器足够聪明,可以在编译的字节码中内联调用,因此两种方法的性能相似,而第二种方法更好,因为它提供了封装。