Java的SDK接口常量声明示例

Interface constant declaration example in Java's SDK

Java 的内置库 (JDK) 中是否有包含常量字段的接口示例?

documentation开始,可以在接口中定义常量声明,但我不记得见过这样的。

public interface OperateCar {

   // constant declarations, if any
   ...
}

您的 interface 的每个字段都是隐含的 public static final,因此使其成为常量。

所以:

public interface MyInterface {
    String FOO = "foo";
}

... 等同于:

public interface MyInterface {
    public static final String FOO = "foo";
}

例如

package java.text;

public interface CharacterIterator extends Cloneable
{
    /**
     * Constant that is returned when the iterator has reached either the end
     * or the beginning of the text. The value is '\uFFFF', the "not a
     * character" value which should not occur in any valid Unicode string.
     */
    public static final char DONE = '\uFFFF';

但通常很难在 JDK 接口中找到常量,因为它不符合语言约定。