Menu

#880 False +: AvoidInstantiatingObjectsInLoops on for loop expr.

open
pmd (543)
5
2012-10-07
2009-07-06
Nick Radov
No

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);
    }
}

}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.