articles

Home / DeveloperSection / Articles / Annotations in Java: @Deprecated

Annotations in Java: @Deprecated

Jonas Stuart 2135 28-May-2016

The Java API over the years has deprecated quite a few classes and methods. This means new methods and classes have been added that provide a better way to achieve the same task; therefore, the deprecated classes and methods need not be used any more by developers. Such entities are marked with the @Deprecated annotation. One such class is StringBufferInputStream.

Let’s see what happens when we use this class in the program code here:

public class DeprecatedAnnotation {               

           public static void main(String[] args) {
                                             java.io.StringBufferInputStream in =
                                                              new java.io.StringBufferInputStream("A sample string");
           }
}

 We compile this code with the following command line:

javac -Xlint:deprecation DeprecatedAnnotation.java

The compiler output is shown here:

DeprecatedAnnotation.java:9: warning: [deprecation] StringBufferInputStream in java.io has been deprecated
java.io.StringBufferInputStream in =

^
DeprecatedAnnotation.java:10: warning: [deprecation] StringBufferInputStream in java.io has been deprecated new java.io.StringBufferInputStream("A sample string");
^
2 warnings


The compiler generates two warnings, showing us the lines in our program where we have used the deprecated class.

We can even use this @Deprecated annotation in our own code for marking the elements we want to phase out eventually. Consider the code shown here:  

public class DeprecatedAnnotationDemo {

                     public static void main(String[] args) {
                             MyTestClass testObject = new MyTestClass();
                             testObject.doSomething();
                             testObject.doSomethingNew("Bowling");
                     }
}
class MyTestClass {
                     @Deprecated
                     public void doSomething() {
                     }                      public void doSomethingNew(String SomeFun) {
                     }
}

 

Here, we have defined a class called MyTestClass with two methods. In this scenario, we initially have only one method in this class, called doSomething. Later on, though, we decide to provide a better implementation for this method, which requires sending a String-type parameter to the method. Therefore, we write another method called doSomethingNew that takes a String parameter. In this test application, we call both the old and new methods. Now, when we compile this code, the compiler generates the following output:

javac -Xlint:deprecation DeprecatedAnnotationDemo.java
DeprecatedAnnotationDemo.java:10: warning: [deprecation] doSomething() in
MyTest Class has been deprecated
testObject.doSomething();
^
1 warning


 
Note that the -Xlint switch is used on the command line to get warning errors on deprecated elements. In this situation, the compiler gave us the warning that the doSomething method has been deprecated. This is how we’ll use the @Deprecated annotation in our classes to mark any methods we do not want our developers to use in their future code while ensuring at the same time that their existing code does not break. Note that this @Deprecated annotation can also be applied to classes like the one we’ve seen for the StringBufferInputStream class. This is the mechanism Java uses to discourage developers from using certain classes and methods in their future code.


Updated 05-Jul-2018

Leave Comment

Comments

Liked By