-2

i have a question in effective_java this book, what's mean of the 'modern Java virtual machine (JVM) implementations are almost certain to inline the call to the static factory method.' i don't understand 'inline the call to the static factory method'

Quote:

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

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
sunh
  • 74
  • 10
  • 1
    It would really help to have a bit more of the context. It is ambiguous whether the author is speaking about compilation to byte-code, JIT-compilation from byte-code, or certain byte-code optimizations that VMs may make during interpretation. – lockcmpxchg8b Nov 20 '17 at 03:49
  • 1
    If you're quoting from a book - please provide the full quote and additional context if needed. – Nir Alfasi Nov 20 '17 at 03:51
  • This is a bit off topic but that book is for seasoned java programmers – Digvijaysinh Gohil Nov 20 '17 at 03:54
  • '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. – sunh Nov 20 '17 at 03:54
  • this is about singleton. – sunh Nov 20 '17 at 04:00
  • this is my first question in stackoverflow,,so I'm a little excited,,thank you everyone. – sunh Nov 20 '17 at 04:04
  • Welcome to StackOverflow. Before posting your next question, please visit the [help] and read [ask] to learn the guidelines for asking questions here. – Jim Garrison Nov 20 '17 at 04:19
  • ok,i will do this – sunh Nov 20 '17 at 04:30

1 Answers1

1

The "public field approach" is (taken from the book "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() { ... }
}

While the approach you're referring to in the quote is the static factory:

// 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() { ... }
}

And the quote you mentioned explains that the "performance penalty" for the static factory approach (because we're calling a method getInstance instead of using the field directly via: Elvis.INSTANCE) no longer exist (or, very low chance that it does) since the compiler is smart enough to inline the call in the compiled bytecode, so the performance of both approaches is similar while the second approach is better since it provides encapsulation.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129