Is it possible to deprecate some of the values of a Java enum and if so, how?

Steve Cohen picture Steve Cohen · Sep 12, 2011 · Viewed 11.4k times · Source

I want to deprecate some, but not all possible enumeration values.

Answer

Jesper picture Jesper · Sep 12, 2011

Yes, put a @Deprecated annotation on them. For example:

enum Status {
    OK,
    ERROR,

    @Deprecated
    PROBLEM
}

You can also add a JavaDoc @deprecated tag to document it:

enum Status {
    OK,
    ERROR,

    /**
     * @deprecated Use ERROR instead.
     */
    @Deprecated
    PROBLEM
}