Inspired by another question, I started wondering about the well-definedness of the following Java code:
public class Test {
List<Object> foo;
Object bar() {
foo = new ArrayList<Object>():
return(/* Anything */);
}
void frob() {
foo.add(bar());
}
}
In this case, does the Java specification specify a strict order of evaluation for the dot operator in frob
? Is left-to-right evaluation guaranteed such that the add
method is always executed on the list that was there before bar
replaces the field with a new list, or can there legally be compilers that let bar
execute before the foo
field is fetched in frob
, thus executing the add
method on the new list?
I suspect that left-to-right evaluation is guaranteed, but is there wording in the specification to this effect?