Given is the following Java code example:
builder.something()
.somethingElse()
.somethingMore(builder.getSomething());
Is it guaranteed by the Java Language Specification that getSomething()
is invoked after the somethingElse()
method or is a Java implementation allowed to reorder the execution?
The JLS, Section 15.12.4, guarantees that the target reference is computed before arguments are evaluated.
At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument expressions are evaluated. ...
The somethingElse
method must be evaluated first, to compute the target reference for the somethingMore
method. Then builder.getSomething()
is evaluated to supply a value for the parameter to somethingMore
. Then somethingMore
can be executed.
Because of this rule, JVMs are not allowed to reorder the execution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With