Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare or mark a Java method as deprecated?

I would like to make one of my methods "deprecated" = not used anymore.

But still I would like to have it in my API. I just want to show "warning" to anyone using that method.

How can I achieve that?

like image 592
Pavel Janicek Avatar asked Jan 27 '12 10:01

Pavel Janicek


People also ask

How do you mark a method as deprecated in Java?

Use both @Deprecated annotation and the @deprecated JavaDoc tag. The @deprecated JavaDoc tag is used for documentation purposes. The @Deprecated annotation instructs the compiler that the method is deprecated.

How do you mark a class deprecated?

You need to use the [Obsolete] attribute. Example: [Obsolete("Not used any more", true)] public class MyDeprecatedClass { //... }

Can we use deprecated methods in Java?

You can still use deprecated code without performance being changed, but the whole point of deprecating a method/class is to let users know there's now a better way of using it, and that in a future release the deprecated code is likely to be removed.


2 Answers

Use @Deprecated on method. Don't forget about clarifying javadoc field:

/**  * Does some thing in old style.  *  * @deprecated use {@link #new()} instead.    */ @Deprecated public void old() { // ... } 
like image 119
Vladimir Ivanov Avatar answered Nov 07 '22 10:11

Vladimir Ivanov


Use both @Deprecated annotation and the @deprecated JavaDoc tag.

The @deprecated JavaDoc tag is used for documentation purposes.

The @Deprecated annotation instructs the compiler that the method is deprecated. Here is what it says in Sun/Oracles document on the subject:

Using the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element. In contrast, there is no guarantee that all compilers will always issue warnings based on the @deprecated Javadoc tag, though the Sun compilers currently do so. Other compilers may not issue such warnings. Thus, using the @Deprecated annotation to generate warnings is more portable that relying on the @deprecated Javadoc tag.

You can find the full document at How and When to Deprecate APIs

like image 20
ShaMan-H_Fel Avatar answered Nov 07 '22 10:11

ShaMan-H_Fel