Java: force implementor of an interface to have an immutable name field?
<p>Requirement: I'd like all implementations of an interface to have a well-defined name.</p>

<p>Initially, I thought:</p>

<pre><code>interface Fruit {
    public String getName();
}
</code></pre>

<p>But this allows the user to have a field that is modified at run-time. I want to have an immutable name that is defined before compile/build time.</p>

<p>I've been toying with a couple of other ways to do it, but each has a limitation.</p>

<p>1) Give the name a type, which has slightly more control than free-form strings:</p>

<pre><code>interface Fruit {
    public FruitName getName();
}

abstract class FruitName  {
    public final String NAME;
    public FruitName(name) {
        this.NAME = name;
    }
}
</code></pre>

<p>A user of this class will look like this:</p>

<pre><code>class AppleFruitName extends FruitName {
    public AppleFruitName() {
        super("apple");
    }
}

class Apple implements Fruit {
    public FruitName getName() {
        return new AppleFruitName();
    }
}
</code></pre>

<p>2) Force an implementor of <code>Fruit</code> to annotate the name with something:</p>

<pre><code>class Apple implements Fruit {
    @FruitName
    public static final NAME = "apple";
    ...    
}
</code></pre>

<p>Clearly this implementation is far cleaner than (1), but I'm not sure if this is possible in Java? How do you get compile/build to fail if @FruitName is not present?</p>
