CODSTA.ISACF
Avoid using an "interface" to define constants
Description
This rule flags code where an "interface" is used to define a constant.
An interface should not be used to define constants. While it is common practice to use a constant interface (an interface that only contains static final fields and no methods) this should be avoided. The use of constants is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API. In order to avoid this, constants should be moved to a utility class, a class which only contains static variables and methods.
Note: Jtest's CODSTA.DCI rule conflicts with this rule. Because we have seen arguments for both guidelines, we have included both rules and will allow you to decide which one to use.
Example
package CODSTA;
public interface ISACF {
int NUM = 1234; // violation
}
Repair
Place constants in a utility class instead of an "interface"
package CODSTA;
public class ISACF_CLASS {
private ISACF_CLASS() {} // Prevents instantiation
static final int NUM = 1234;
}
Reference
Bloch, Joshua. Effective Java Programming Language Guide. Addison Wesley, 2001, pp 89 - 90.
|