开发者

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 va开发者_JS百科lues.


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
}


public enum Characters {
    STAN,
    KYLE,
    CARTMAN,
    @Deprecated KENNY
}


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;
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜