Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possibility to deprecate enum constants?

Is it possible to use the @Deprecated annotation on enum constants?

like image 417
Azoraqua Avatar asked Jan 07 '23 03:01

Azoraqua


2 Answers

If you check the javadoc, you will see the declaration:

@Target(value={CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE})

which includes FIELD. If you click on FIELD you get to its javadoc which states:

Field declaration (includes enum constants)

So the answer is: yes it should work fine.

like image 72
assylias Avatar answered Jan 26 '23 22:01

assylias


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

public enum Status {
OK,
ERROR,

@Deprecated
PROBLEM
}

@Deprecated is an annotation that is read by the compiler, used to mark a method as deprecated to the compiler and will generate a deprecation compile-time warning if the method is used.

@deprecated is a javadoc tag used to provide documentation about the deprecation. You can use it to explain why the method was deprecated and to suggest an alternative. It only makes sense to use this tag in conjunction to the @Deprecated annotation.

like image 45
Abdelhak Avatar answered Jan 26 '23 22:01

Abdelhak