我应该在 Java 中使用 JavaDoc 弃用还是注释?

目前有两种方法可以将代码标记为在 java 中被破坏的。

通过 JavaDoc

/**
* @deprecated
*/

或者作为一个注释:

@Deprecated

这就是我的问题所在——当在使用 Eclipse 时将一个方法标记为已弃用时,我发现同时声明这两个方法有点太多了。我真的只想用其中一个。

然而,使用注释能给编译器提供实际有用的附加信息吗?

但是只使用注释,我不能说明为什么这个方法被弃用-我只能用 JavaDoc 来做这件事,而不指定为什么就弃用一个方法是不好的。

那么,我只能使用其中的一个吗? 或者我真的应该学会同时指定两个吗?

25762 次浏览

You should use both. The Annotation allows the compiler to display a warning whenever a deprecated method is used, and the javadoc explains why. Both are important.

As per Oracle's Java Annotations tutorial:

When an element is deprecated, it should also be documented using the Javadoc @deprecated tag...

You should specify both.

The annotation lets the compiler know about it and trigger warnings when the method is used. The JavaDoc attribute lets developers know about before they start using it.

These are two very different things!

From the horse's mouth:

NOTE: The Java Language Specification requires compilers to issue warnings when classes, methods, or fields marked with the @Deprecated annotation are used. Compilers are not required by the Java Language Specification to issue warnings when classes, methods, or fields marked with the @deprecated Javadoc tag are accessed, although the Sun compilers currently do so.

So basically, if you want a guarantee that there will be compiler warnings, you need to use the annotation. And because of some API designer's breathtaking incompetence, you need to specify the javadoc tag as well to give an explanation.

Personally, I'd say the annotation is useless and should be omitted until it's fixed, since any good compiler or IDE will display warnings with the javadoc tag as well.

You should write both. The @Deprecated Anotation is for the Compiler and the @deprecated JavaDoc tag is for the Person who wants to know why this is deprecated.

An example can look like this:

/**
* @deprecated We dont need this Method because ...
*/
@Deprecated
public void doStuff(){..}

This can be easily dealt with a good IDE.

Eclipse Neon, for eg. automatically adds @Deprecated annotation, when I create a javadoc @deprecated on a method or field.

So I simply write the javadoc with the appropriate explanation and let the IDE take care of adding the @Deprecated annotation, the minute I save the file.