This code generates an AvoidInstantiatingObjectsInLoops warning on the expression inside the enhanced for loop. The warning is incorrect because only a single object is instantiated. It isn't creating a new object on every pass through the loop.
package org.sourceforge.pmd;
import java.util.ArrayList;
public class AvoidInstantiatingObjectsInLoops extends ArrayList<String> {
private AvoidInstantiatingObjectsInLoops() {
add("foo");
add("bar");
}
public static void main(final String[] args) {
for (final String s : new AvoidInstantiatingObjectsInLoops()) {
System.out.println(s);
}
}
}