Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 521
Steve Cohen Avatar asked Sep 12 '11 18:09

Steve Cohen


People also ask

Can we change enum values in Java?

Enum constants are implicitly static and final and you can not change their value once created. Enum in Java provides type-safety and can be used inside switch statements like int variables.

Can we override enum in Java?

Enums are exactly final inner classes that extends java. lang. Enum<E> . You cannot extend, override or inherit an enum .

Can enum be subclassed?

We've learned that we can't create a subclass of an existing enum. However, an interface is extensible. Therefore, we can emulate extensible enums by implementing an interface.


1 Answers

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 } 
like image 52
Jesper Avatar answered Sep 28 '22 03:09

Jesper