I've got an issue with the PMD rule Avoid instantiating new objects inside loops
. Here is some example code:
import java.awt.Dimension;
public class PMDDemo {
public static void main(final String[] args) {
final Dimension[] arr = new Dimension[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Dimension(i, i); // rule violation here
}
}
}
PMD gives me the above mentioned rule violation at the marked spot in the code. How am I supposed to create n instances of a class without creating them within a loop?
I know that some of PMD's rules are controversial (like the onlyOneExit rule). But up to now I at least understood the idea behind them. I don't understand the reasoning behind this rule. Can someone help me with that?