Over the time libraries, frameworks and APIs change. With every change comes the easy and simpler way of writing code. Whenever there is a change, we might not want people to use our old program elements like methods, constructors, fields and types. Such elements may be annotated as @Deprecated
.
@Deprecated
annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever that element (element annotated with @Deprecated
) is used.
@Deprecated annotation tells users not to use the annotated element. Whenever we use @Deprecated, it better to tell users what is the best alternative of this element which will serve the purpose.
When an element is deprecated, it should also be documented using the Javadoc @deprecated
tag.
class Employee{ /** * Get variable salary of employee * @deprecated * This method is no longer valid to get variable salary of employee. * <p> Use getVariableSalaryNew() instead. * */ @Deprecated public void getVariableSalary() { System.out.println("this is getVariableSalary method"); } public void getVariableSalaryNew() { System.out.println("this is new getVariableSalary method"); } } public class Test { public static void main(String[] args) { Employee emp = new Employee(); emp.getVariableSalary(); } }
When you compile the code, you should get like the following:
Note: Test.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.