Beginners Java people will inadvertently define the constants in the interface, the only reason is that the interface can not be instantiated, and the use of the interface defined by the constant is not attached to the instance. This is mainly the JDK itself gives us a lot of such examples, such as Java.io.ObjectStreamConstans, mostly appear before the Enum type arrives.
In fact, Java interface constants are an anti-pattern for the following reasons:
1. Interfaces cannot be prevented from being implemented or inherited, that is, a sub-interface or implementation is the definition of a constant that can be overridden so that a reference to a constant through the parent, subinterface (or implementation) can be inconsistent
2. Similarly, due to being implemented or inherited, a large number of interfaces, classes, or instances can be used to refer to the same constant in the inheritance tree, resulting in the constants defined in the interface polluting the namespace. (The Java compiler actually allows the use of instances to reference class variables)
3. The implication of an interface is that it needs to be implemented and represents a type whose public member is the API to be exposed. The constants defined in the interface are not the API
See also: Effective Java 19th: interface for defining types only
Since the interface is not suitable for defining constants, where is the constant home? Interfaces are built for implementation/inheritance, if placed in a class, and the class is final, and the construction method is closed. So our previous interface constants were defined
Read the full text
Java interface constant anti-pattern and how to define Java constants