AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 36001

Interface constant declaration example in Java's SDK

Is there an example of an Interface in Java's built-in library (JDK) that contains a constant field?

From the documentation, constant declaration can be defined in interfaces, but I can't remember seeing such.

public interface OperateCar {

   // constant declarations, if any
   ...
}

Upvotes: 0

Views: 70

Answers (2)

AdamSkywalker
AdamSkywalker

Reputation: 11609

For example

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';

But generally, it is hard to find constants in JDK interfaces, since it does not fit the language convention.

Upvotes: 2

Mena
Mena

Reputation: 48404

Every field of your interface is implicitly public static final, thus making it a constant.

So:

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

... is the same as:

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

Upvotes: 1

Related Questions