Java SDK中的接口常量声明示例

时间:2015-06-15 08:59:37

标签: java interface constants

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

documentation开始,可以在界面中定义常量声明,但我不记得看到这样的声明。

public interface OperateCar {

   // constant declarations, if any
   ...
}

2 个答案:

答案 0 :(得分:2)

例如

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接口中很难找到常量,因为它不符合语言约定。

答案 1 :(得分:1)

interface的每个字段都隐含public static final,从而使其成为常量。

所以:

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

...与:

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