How and When to Deprecate methods in Java programming language


What are Annotations in Java ?.

Annotations in Java are metadatas that adds information to your code in a formal way so that you can keep track to the information added at a later stage. They provide a Java programmer information which describes the program fully in a sophisticated way. Annotations help us in generating descriptor files and giving us way to write boilerplate code each time.

Annonations in Java have a symbol @ in its syntax declaration. Java contains general purpose built in annotations defined in java.lang package as :

@Deprecated : Generally, it gives a programmer a compiler warning if he wishes to use a thing which is marked as Deprecated.




Program to demonstrate how to deprecate methods in Java

package com.hubberspot.example;

public class DeprecateTest {
   // One way do deprecate is to use 
   // @Deprecated annotation 
   @Deprecated
   public void draw() {
 System.out.println("Deprecated by @Deprecated annotation");
   }

   /**
    * @deprecated
    * 
    */
    public void area() {
 System.out.println("Deprecated by @deprecated javadoc tag");
    }

    public static void main(String[] args) {

 DeprecateTest test = new DeprecateTest();
 // It might be unavailable in next release
 // say method is inefficient, having bugs,
 // insecure in implementation and may result
 // in bad coding practices   
 test.draw();
 test.area();

   }

}




Output of the program :