Always reuse immutable constant objects for better memory utilization
Severity: Medium
Rule: Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption.
Reason: Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption.
Rule: Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption.
Reason: Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption.
Usage Example:
public class Test { protected Object[] getObjects() { return new Object[0]; // VIOLATION } publicstatic Integer convertToInt(String s) { if (s == null || s.length() == 0) { return new Integer(-1); // VIOLATION } else { return new Integer(s); } } }
Should be written as:
public class Test { public static final Object[] NO_OBJECTS = new Object[0]; protected Object[] getObjects() { return NO_OBJECTS; // FIXED } private static final Integer INT_N1 = new Integer(-1); public static Integer convertToIn(String s) { if (s == null || s.length() == 0) { return INT_N1; // FIXED } else { return new Integer(s); } } }
Reference: Not available.
Comments
Post a Comment