×
☰ See All Chapters

@Deprecated annotation in Java 9

In java 9, two methods added to this Deprecated interface, they are forRemoval and since. Both are optional elements for Deprecated annotation. Till java 1.8 Deprecated annotation was a marker interface. A marker interface is an interface that has no members inside it. It is used provide information about the object compiler and JVM.

Modifier and Type

Method

Description

boolean

forRemoval

Indicates whether the annotated element is subject to removal in a future version.

String

since

Returns the version in which the annotated element became deprecated.

 

package com.java4coding.test;

 

class Demo {

        @Deprecated(forRemoval = true, since = "2")

        int var = 100;

       

        @Deprecated(forRemoval = true, since = "2")

        public void meth() {

        }

}

 

@Deprecated(forRemoval = true, since = "2")

class DeprecatedClass {

        @Deprecated(forRemoval = true, since = "2")

        public void meth() {

        }

}

 

public class Test {  

    public static void main(String[] args) {

            Demo demo = new Demo();

            demo.meth();

            System.out.println(demo.var);

           

            DeprecatedClass obj =  new DeprecatedClass();

            obj.meth();

    }  

}  

 

@Deprecated annotation is used for informing the user of any deprecated class, field, method, interface, constructor, enum etc..  by showing warning from compiler.  When user tries to use any member which is annotated with @Deprecated, compiler shows a warning message to user.  Eclipse IDE makes your code strikeout when you try to use deprecated class, class members.

Developers use @Deprecated annotation to mark their code as deprecated and inform the users of their code that they may stop providing support for that deprecated code or they may remove those in future. Hence users should update their code by stop using the deprecated code or by using any alternate code.

 

deprecated-annotation-in-java-0
 

 


All Chapters
Author