52

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • 1
    See [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 Answers3

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
  • 5
    I'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
63
public enum Characters {
    STAN,
    KYLE,
    CARTMAN,
    @Deprecated KENNY
}
Barend
  • 17,296
  • 2
  • 61
  • 80
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