Java method call order using chaining methods

This is the following Java code example:

builder.something() .somethingElse() .somethingMore(builder.getSomething()); 

Is the Java language specification guaranteed that getSomething() is called after the somethingElse() method or is it a Java implementation that allows you to change the order of execution?

+5
source share
1 answer

JLS, section 15.12.4 , ensures that the target link will be evaluated before the arguments are evaluated.

When you start a method call, five steps are required. First, the target link can be computed. Secondly, argument expressions are computed ....

First, the somethingElse method must be evaluated to calculate the target link for the somethingMore method. Then builder.getSomething() is evaluated to supply the value for the somethingMore parameter. Then somethingMore can be executed.

Because of this rule, JVMs cannot change the order of execution.

+5
source

Source: https://habr.com/ru/post/1234837/


All Articles