Javatpoint Logo
Javatpoint Logo

The Deprecated Annotation in Java

In Java, the Deprecated annotation can be defined as an annotation that is utilized for indicating that a specific class, method, interface, or field should not be available to use. A deprecated element or entity is marked for indicating that is not available for usage anymore.

What is Deprecated Annotation in Java

As explained in the introduction, a deprecated element such as a class, field, method, or interface cannot be used by anyone. If an element gets deprecated, it becomes irrelevant. In simple words, a deprecated element is considered unimportant or useless.

The compiler gets notified by the deprecated annotation when any element or entity is deprecated, and the compiler will generate and throw a warning if any user tries to use the deprecated element.

Why Do We Use Deprecated Annotations in Java

Every program contains certain kinds of API for functioning. Over time, the Application Programming Interface of the project will change and evolve. Specific elements in the project such as methods, fields, classes, constructors, or types are not required to be used by the people or end-users.

The deprecated annotation in Java can be utilized for marking these specific elements as deprecated. Once an element is marked as deprecated, other users or developers get indicated that the marked element should not be used.

How To Deprecate an Element in Java?

The Deprecated annotation is majorly utilized for deprecating any element in Java.

In Java, an interface, class, method, members of class and constructor can be deprecated using the @Deprecated annotation.

Implementing Deprecation Annotation in Java

Let us understand more about Deprecation Annotation in Java with the help of a few example Java programs.

Example 1:

In this Java example program, we will be learning how to use a deprecated variable.

Filename: DeprecatedAnnotation1.java

Output:

50000

Example 2:

In this Java example program, we will be learning how to use a deprecated method.

Filename: DeprecatedAnnotation2.java

Output:

THIS METHOD IS DEPRECATED

Example 3:

In this Java example program, we will be learning how to use a deprecated variable and a deprecated method.

Filename: DeprecatedAnnotation3.java

Output:

THIS METHOD IS DEPRECATED
47

Example 4:

In this Java example program, we will be learning how to use a deprecated variable and a deprecated constructor.

Filename: DeprecatedAnnotation4.java

Output:

This Constructor is DEPRECATED
47






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA