I want to deprecate some, but not all possible enumeration values.
Asked
Active
Viewed 1.7k times
52
-
1See [Annotations tutorial](http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html). – mre Sep 12 '11 at 18:33
-
I think it is about time to accept an answer here, I really doubt that much else will be showing up here ;-) – GhostCat Aug 30 '19 at 08:41
3 Answers
76
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
}

Jesper
- 202,709
- 46
- 318
- 350
-
5I've always (well, occasionally) wondered why the `@Deprecated` annotation type doesn't allow a `value` for that purpose. – Barend Sep 12 '11 at 18:38
-
1"Don't use this value" is a useless comment. It doesn't say anything other than the deprecated annotation. A deprecation comment should tell by what the value is replaced. – JB Nizet Sep 12 '11 at 18:39
-
@JB Nizet this was ofcourse only to demonstrate the principle. I changed it. – Jesper Sep 12 '11 at 18:39
-
Yes, I have never used annotations much. I am more familiar with deprecation via comments. the @Deprecated solution is just what I was looking for, thanks. – Steve Cohen Sep 12 '11 at 19:06
5
Just tried it eclipse, it works:
public class Test {
public static void main(String[] arg) {
System.err.println(EnumTest.A);
System.err.println(EnumTest.B);
}
public static enum EnumTest {
A, @Deprecated B, C, D, E;
}
}

Baum mit Augen
- 49,044
- 25
- 144
- 182

home
- 12,468
- 5
- 46
- 54