If there is a method call MyClass.returnArray() and I iterate over the array using the for-each construct (also called the “enhanced for” loop):
for (ArrayElement e : MyClass.returnArray()) {
//do something
}
will then the returnArray() method be called for every iteration?
No, it won’t. The result of the first call will be stored in the compiled code in a temporary variable.
From Effective Java 2nd. Ed., Item 46: