Constant Interface Anti-Pattern

Don’t do this and don’t implement it for your class.

// Don't do this.
public interface Constants {
    public static final int SOME_CONSTANT = 1;
}

// Don't implement it.
public class YourClass implements Constants { ... }

It is bad because:

  • A public constant is a part of public API and a (serious) commitment.
  • Implementing random interfaces pollutes your class’ and sub-classes’ name space (use static import instead).
  • In general, interface should carry an intent or information of type hierarchy.
Creative Commons License
This blog by Che-Liang Chiou is licensed under a Creative Commons Attribution 4.0 International License.