OPT.LOOP: Avoid instantiating variables in the loop body
DESCRIPTION
This rule flags any variable that is instantiated in the loop body.
Instantiating temporary Objects in the loop body can increase the memory management overhead.
EXAMPLE
package OPT;
import java.util.Vector;
public class LOOP {
void method (Vector v) {
for (int i=0;i < v.isEmpty();i++) {
Object o = new Object();
o = v.elementAt(i);
}
}
}
DIAGNOSIS
Variable instantiated in loop body: o
REPAIR
Declare a variable outside of the loop and reuse this variable.
package OPT;
import java.util.Vector;
public class LOOP {
void method (Vector v) {
Object o;
for (int i=0;i<v.isEmpty();i++) {
o = v.elementAt(i);
}
}
}
|