301

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?

Adi
  • 4,989
  • 6
  • 30
  • 47
Pavel Janicek
  • 12,577
  • 12
  • 47
  • 76

6 Answers6

607

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() {
// ...
}
Clijsters
  • 3,240
  • 1
  • 22
  • 35
Vladimir Ivanov
  • 41,277
  • 17
  • 74
  • 99
  • 2
    How do you link an external library? eg: com.hello.api.PublicController#new – Faizan Kazi Apr 07 '17 at 05:57
  • @LinuxLars completely agreed! Java 9 added a couple of attributes to start making deprecation be taken seriously but adding another attribute `reason` with a default value of `""` couldn't have hurt – asgs Jan 31 '18 at 08:48
  • 3
    I'd wish the `@deprecated` message in the comment could be added to `@Deprecated` (one spot to fix them all)... – U. Windl May 16 '18 at 12:12
90

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

Mr. Polywhirl
  • 31,606
  • 11
  • 65
  • 114
ShaMan-H_Fel
  • 2,068
  • 18
  • 24
  • 1
    Not quite true. *Both* javadoc and annotation tell compiler method is deprecated – Bohemian Jan 27 '12 at 14:44
  • 18
    @Bohemian Actually that is not quite true. The annotation is defined in the Java Language Specification section 9.6.1.6 (http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.6.1.6), while the javadoc tag is not. So the annotation is part of the language. If you decide to write your own Java compiler, you may ignore the javadoc tag, but you must recognize the annotation. – ShaMan-H_Fel Jan 27 '12 at 15:05
  • @ShaMan-H_Fel I believe the javadoc model works too. Because it was the only choice before Java 5, and it did work. When you marked a method with `@deprecated` javadoc tag (in Java 4-), the compiler marked the method (class, field) as deprecated and the IDEs showed warnings, even when no source was available. – Amir Pashazadeh Sep 16 '17 at 13:26
47

since some minor explanations were missing

Use @Deprecated annotation on the method like this

 /**
 * @param basePrice
 * 
 * @deprecated  reason this method is deprecated <br/>
 *              {will be removed in next version} <br/>
 *              use {@link #setPurchasePrice()} instead like this: 
 * 
 * 
 * <blockquote><pre>
 * getProduct().setPurchasePrice(200) 
 * </pre></blockquote>
 * 
 */
@Deprecated
public void setBaseprice(int basePrice) {
}

remember to explain:

  1. Why is this method no longer recommended. What problems arise when using it. Provide a link to the discussion on the matter if any. (remember to separate lines for readability <br/>
  2. When it will be removed. (let your users know how much they can still rely on this method if they decide to stick to the old way)
  3. Provide a solution or link to the method you recommend {@link #setPurchasePrice()}
azerafati
  • 15,900
  • 6
  • 60
  • 65
37

There are two things you can do:

  1. Add the @Deprecated annotation to the method, and
  2. Add a @deprecated tag to the javadoc of the method

You should do both!

Quoting the java documentation on this subject:

Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead.

Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used. The compiler suppresses deprecation warnings if a deprecated compilation unit uses a deprecated class, method, or field. This enables you to build legacy APIs without generating warnings.

You are strongly recommended to use the Javadoc @deprecated tag with appropriate comments explaining how to use the new API. This ensures developers will have a workable migration path from the old API to the new API

Community
  • 1
  • 1
Bohemian
  • 365,064
  • 84
  • 522
  • 658
8

Use the annotation @Deprecated for your method, and you should also mention it in your javadocs.

amit
  • 166,614
  • 24
  • 210
  • 314
3

Take a look at the @Deprecated annotation.

jham
  • 355
  • 1
  • 6