Java @Deprecated annotation examples

Java @deprecated examples:.

  • Deprecating a type (class or interface): Deprecated interface: @Deprecated interface Kool { // interface methods } Deprecated class: @Deprecated class Foo { // class implementation }
  • Deprecating a method: class Bar { @Deprecated public void doSomethingWeird() { // old method } public void doSomethingCool() { // new, alternate method } }
  • Deprecating a member variable (field): class Constant { @Deprecated public static final int MAX_SIZE = 1024; // new, alternate field public static final int MAX_UPLOAD_SIZE = 1024; }
  • Deprecating a constructor: class Car { @Deprecated Car(String color, int length, int width) { } // new, alternate constructor Car(Style style) { } }

Some Rules about @Deprecated annotation:

  • When a type, method, field or constructor is annotated with the @Deprecated  a nnotation, the compiler will issue a deprecation warning if the deprecated element is used (e.g. invoked, referenced, or overridden). For example, if we call the doSomethingWeird() of the Bar class above like this: Bar b = new Bar(); b.doSomethingWeird(); The compiler will complain like this: Note: Path\to\java\file.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.  
  • The deprecated element is also used in another deprecated element, e.g. a deprecated class invokes a deprecated method.
  • The deprecated element is used within an entity which is annotated by the @SuppressWarnings(“deprecation”) annotation.
  • The deprecated element is declared and used both within the same outermost class. For example: class MyCollection { @Deprecated public static final int DEFAULT_CAPACITY = 10; private int initialCapacity = DEFAULT_CAPACITY; }
  • The @Deprecated  annotation has no effect on local variable or parameter declaration.

void getBytes (int srcBegin, int srcEnd, byte[] dst, int dstBegin)

Deprecated.  

This method does not properly convert characters into bytes. As of JDK 1.1, the preferred way to do this is via the getBytes() method, which uses the platform's default charset.

Other Annotations in Core Java:

  • Java @Override annotation examples
  • Java @SuppressWarnings annotation examples

Recommended Java Tutorials:

  • 12 Rules and Examples About Inheritance in Java
  • 12 Rules of Overriding in Java You Should Know
  • 10 Java Core Best Practices Every Java Programmer Should Know
  • Understanding Object Ordering in Java with Comparable and Comparator
  • Understanding equals() and hashCode() in Java

About the Author:

what is @deprecated annotation in java

Add comment

   

Notify me of follow-up comments

Comments  

  • Watch & Listen
  • Oracle University

Annotations

Annotations have a number of uses, among them:

  • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime.

This section explains where annotations can be used, how to apply annotations, what predefined annotation types are available in the Java Platform, Standard Edition (Java SE API), how type annotations can be used in conjunction with pluggable type systems to write code with stronger type checking, and how to implement repeating annotations.

The Format of an Annotation

In its simplest form, an annotation looks like the following:

The at sign character ( @ ) indicates to the compiler that what follows is an annotation. In the following example, the annotation's name is Override :

The annotation can include elements , which can be named or unnamed, and there are values for those elements:

If there is just one element named value , then the name can be omitted, as in:

If the annotation has no elements, then the parentheses can be omitted, as shown in the previous @Override example.

It is also possible to use multiple annotations on the same declaration:

If the annotations have the same type, then this is called a repeating annotation:

Repeating annotations are supported as of the Java SE 8 release. For more information, see the section Repeating Annotations.

The annotation type can be one of the types that are defined in the java.lang or java.lang.annotation packages of the Java SE API. In the previous examples, Override and SuppressWarnings are predefined Java annotations. It is also possible to define your own annotation type. The Author and Ebook annotations in the previous example are custom annotation types.

Where Annotations Can Be Used

Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used on a declaration, each annotation often appears, by convention, on its own line.

As of the Java SE 8 release, annotations can also be applied to the use of types. Here are some examples:

Class instance creation expression:

implements clause:

Thrown exception declaration:

This form of annotation is called a type annotation .

Declaring an Annotation Type

Many annotations replace comments in code.

Suppose that a software group traditionally starts the body of every class with comments providing important information:

To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:

The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign ( @ ) ( @ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later section. For the moment, you do not need to understand interfaces.

The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.

After the annotation type is defined, you can use annotations of that type, with the values filled in, like this:

Note: To make the information in @ClassPreamble appear in Javadoc-generated documentation, you must annotate the @ClassPreamble definition with the @Documented annotation:

Predefined Annotation Types

A set of annotation types are predefined in the Java SE API. Some annotation types are used by the Java compiler, and some apply to other annotations.

Annotation Types Used by the Java Language

The predefined annotation types defined in java.lang are @Deprecated , @Override , and @SuppressWarnings .

@Deprecated

@Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation. When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example. The use of the at sign ( @ ) in both Javadoc comments and in annotations is not coincidental: they are related conceptually. Also, note that the Javadoc tag starts with a lowercase d and the annotation starts with an uppercase D .

Note that, as of Java SE 9, a forRemoval attribute has been added to the @Deprecated annotation. It indicates whether the annotated element is subject to removal in a future version. The default value is false .

@Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will is discussed in the section Interfaces and Inheritance.

While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

@SuppressWarnings

@SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the following example, a deprecated method is used, and the compiler usually generates a warning. In this case, however, the annotation causes the warning to be suppressed.

Every compiler warning belongs to a category. The Java Language Specification lists two categories: deprecation and unchecked. The unchecked warning can occur when interfacing with legacy code written before the advent of generics. To suppress multiple categories of warnings, use the following syntax:

@SafeVarargs

@SafeVarargs annotation, when applied to a method or constructor, asserts that the code does not perform potentially unsafe operations on its varargs parameter. When this annotation type is used, unchecked warnings relating to varargs usage are suppressed.

@FunctionalInterface

@FunctionalInterface annotation, introduced in Java SE 8, indicates that the type declaration is intended to be a functional interface, as defined by the Java Language Specification.

Annotations That Apply to Other Annotations

Annotations that apply to other annotations are called meta-annotations. There are several meta-annotation types defined in java.lang.annotation .

@Retention annotation specifies how the marked annotation is stored:

  • RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
  • RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
  • RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.

@Documented

@Documented annotation indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.) For more information, see the Javadoc tools page.

@Target annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to. A target annotation specifies one of the following element types as its value:

  • ElementType.ANNOTATION_TYPE can be applied to an annotation type.
  • ElementType.CONSTRUCTOR can be applied to a constructor.
  • ElementType.FIELD can be applied to a field or property.
  • ElementType.LOCAL_VARIABLE can be applied to a local variable.
  • ElementType.METHOD can be applied to a method-level annotation.
  • ElementType.MODULE can be applied to a module declaration.
  • ElementType.PACKAGE can be applied to a package declaration.
  • ElementType.PARAMETER can be applied to the parameters of a method.
  • ElementType.RECORD_COMPONENT can be applied to the component of a record.
  • ElementType.TYPE can be applied to the declaration of a class, an abtract class, an interface, an annotation interface, an enumeration, or a record declaration.
  • ElementType.TYPE_PARAMETER can be applied on the parameters of a type.
  • ElementType.TYPE_USE can be applied where a type is used, for instance on the declaration of a field.

@Inherited annotation indicates that the annotation type can be inherited from the super class. (This is not true by default.) When the user queries the annotation type and the class has no annotation for this type, the class' superclass is queried for the annotation type. This annotation applies only to class declarations.

@Repeatable

@Repeatable annotation, introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same declaration or type use. For more information, see the section Repeating Annotations.

Type Annotations and Pluggable Type Systems

Before the Java SE 8 release, annotations could only be applied to declarations. As of the Java SE 8 release, annotations can also be applied to any type use. This means that annotations can be used anywhere you use a type. A few examples of where types are used are class instance creation expressions ( new ), casts, implements clauses, and throws clauses. This form of annotation is called a type annotation and several examples are provided in the section Annotations Basics.

Type annotations were created to support improved analysis of Java programs way of ensuring stronger type checking. The Java SE 8 release does not provide a type checking framework, but it allows you to write (or download) a type checking framework that is implemented as one or more pluggable modules that are used in conjunction with the Java compiler.

For example, you want to ensure that a particular variable in your program is never assigned to null; you want to avoid triggering a NullPointerException . You can write a custom plug-in to check for this. You would then modify your code to annotate that particular variable, indicating that it is never assigned to null. The variable declaration might look like this:

When you compile the code, including the NonNull module at the command line, the compiler prints a warning if it detects a potential problem, allowing you to modify the code to avoid the error. After you correct the code to remove all warnings, this particular error will not occur when the program runs.

You can use multiple type-checking modules where each module checks for a different kind of error. In this way, you can build on top of the Java type system, adding specific checks when and where you want them.

With the judicious use of type annotations and the presence of pluggable type checkers, you can write code that is stronger and less prone to error.

In many cases, you do not have to write your own type checking modules. There are third parties who have done the work for you. For example, you might want to take advantage of the Checker Framework created by the University of Washington. This framework includes a NonNull module, as well as a regular expression module, and a mutex lock module. For more information, see the Checker Framework .

Repeating Annotations

There are some situations where you want to apply the same annotation to a declaration or type use. As of the Java SE 8 release, repeating annotations enable you to do this.

For example, you are writing code to use a timer service that enables you to run a method at a given time or on a certain schedule, similar to the UNIX cron service. Now you want to set a timer to run a method, doPeriodicCleanup() , on the last day of the month and on every Friday at 11:00 p.m. To set the timer to run, create an @Schedule annotation and apply it twice to the doPeriodicCleanup() method. The first use specifies the last day of the month and the second specifies Friday at 11p.m., as shown in the following code example:

The previous example applies an annotation to a method. You can repeat an annotation anywhere that you would use a standard annotation. For example, you have a class for handling unauthorized access exceptions. You annotate the class with one @Alert annotation for managers and another for admins:

For compatibility reasons, repeating annotations are stored in a container annotation that is automatically generated by the Java compiler. In order for the compiler to do this, two declarations are required in your code.

Step 1: Declare a Repeatable Annotation Type

The annotation type must be marked with the @Repeatable meta-annotation. The following example defines a custom @Schedule repeatable annotation type:

The value of the @Repeatable meta-annotation, in parentheses, is the type of the container annotation that the Java compiler generates to store repeating annotations. In this example, the containing annotation type is @Schedules , so repeating @Schedule annotations is stored in an @Schedules annotation.

Applying the same annotation to a declaration without first declaring it to be repeatable results in a compile-time error.

Step 2: Declare the Containing Annotation Type

The containing annotation type must have a value element with an array type. The component type of the array type must be the repeatable annotation type. The declaration for the @Schedules containing annotation type is the following:

Retrieving Annotations

There are several methods available in the Reflection API that can be used to retrieve annotations. The behavior of the methods that return a single annotation, such as AnnotatedElement.getAnnotation(Class<T>) , are unchanged in that they only return a single annotation if one annotation of the requested type is present. If more than one annotation of the requested type is present, you can obtain them by first getting their container annotation. In this way, legacy code continues to work. Other methods were introduced in Java SE 8 that scan through the container annotation to return multiple annotations at once, such as AnnotatedElement.getAnnotationsByType(Class<T>) . See the AnnotatedElement class specification for information on all of the available methods.

Design Considerations

When designing an annotation type, you must consider the cardinality of annotations of that type. It is now possible to use an annotation zero times, once, or, if the annotation's type is marked as @Repeatable , more than once. It is also possible to restrict where an annotation type can be used by using the @Target meta-annotation. For example, you can create a repeatable annotation type that can only be used on methods and fields. It is important to design your annotation type carefully to ensure that the programmer using the annotation finds it to be as flexible and powerful as possible.

In this tutorial

Last update: September 14, 2021

Java Guides

Java Guides

Search this blog.

Check out my 10+ Udemy bestseller courses and discount coupons: Udemy Courses - Ramesh Fadatare

Java @Deprecated Annotation

what is @deprecated annotation in java

@Deprecated annotation Example

Post a comment.

Leave Comment

Happy New Year 2024! 🎆

Learn spring boot 3, microservices, and full-stack web development using my project-oriented courses.

  • Spring 6 and Spring Boot 3 for Beginners (Includes Projects)
  • Building Real-Time REST APIs with Spring Boot
  • Building Microservices with Spring Boot and Spring Cloud
  • Full-Stack Java Development with Spring Boot 3 & React
  • Testing Spring Boot Application with JUnit and Mockito
  • Master Spring Data JPA with Hibernate
  • Spring Boot + Apache Kafka - The Quickstart Practical Guide
  • Spring Boot + RabbitMQ (Includes Event-Driven Microservices)
  • Spring Boot Thymeleaf Real-Time Web Application - Blog App

Check out my all Udemy courses and updates: Udemy Courses - Ramesh Fadatare

Copyright © 2018 - 2025 Java Guides All rights reversed | Privacy Policy | Contact | About Me | YouTube | GitHub

  • Java Arrays
  • Java Strings
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Spring Boot

Related Articles

  • Solve Coding Problems
  • Coding Guidelines in Java
  • Java Tokens
  • How is Java platform independent?
  • Java Class File
  • Coercion in Java
  • What is Class Loading and Static Blocks in Java?
  • Different Ways to Set a Classpath in Java
  • Print Hello World Without using a Semicolon in Java
  • How is CompileTime classpath different from RunTime classpath in Java?
  • Java Basic Syntax
  • Advantages and Disadvantages of Java
  • Java - symbolic constants
  • Rules For Variable Declaration in Java
  • Introduction to Java
  • Is main method compulsory in Java?
  • Java SAX Library
  • Compilation and Execution of a Java Program
  • Static Block and main() method in Java
  • Java Programming Basics

Annotations in Java

Annotations are used to provide supplemental information about a program. 

  • Annotations start with ‘ @ ’.
  • Annotations do not change the action of a compiled program.
  • Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.
  • Annotations are not pure comments as they can change the way a program is treated by the compiler. See below code for example.
  • Annotations basically are used to provide additional information, so could be an alternative to XML and Java marker interfaces.

Hierarchy of Annotations in Java  

what is @deprecated annotation in java

Implementation:

Note: This program throws compiler error because we have mentioned override, but not overridden, we have overloaded display.

Output:  

If we remove parameter (int x) or we remove @override, the program compiles fine. 

Categories of Annotations

There are broadly 5 categories of annotations as listed:

  • Marker Annotations
  • Single value Annotations
  • Full Annotations
  • Type Annotations
  • Repeating Annotations

Let us discuss and we will be appending code wherever required if so. 

Category 1: Marker Annotations

The only purpose is to mark a declaration. These annotations contain no members and do not consist of any data. Thus, its presence as an annotation is sufficient. Since the marker interface contains no members, simply determining whether it is present or absent is sufficient. @Override is an example of Marker Annotation. 

Category 2: Single value Annotations 

These annotations contain only one member and allow a shorthand form of specifying the value of the member. We only need to specify the value for that member when the annotation is applied and don’t need to specify the name of the member. However, in order to use this shorthand, the name of the member must be a value. 

Category 3: Full Annotations 

These annotations consist of multiple data members, names, values, pairs. 

Category 4: Type Annotations 

These annotations can be applied to any place where a type is being used. For example, we can annotate the return type of a method. These are declared annotated with @Target annotation .

Category 5: Repeating Annotations 

These are the annotations that can be applied to a single item more than once. For an annotation to be repeatable it must be annotated with the @Repeatable annotation, which is defined in the java.lang.annotation package. Its value field specifies the container type for the repeatable annotation. The container is specified as an annotation whose value field is an array of the repeatable annotation type. Hence, to create a repeatable annotation, firstly the container annotation is created, and then the annotation type is specified as an argument to the @Repeatable annotation.

  Predefined/ Standard Annotations

Java popularly defines seven built-in annotations as we have seen up in the hierarchy diagram.

  • Four are imported from java.lang.annotation: @Retention , @Documented , @Target , and @Inherited .
  • Three are included in java.lang: @Deprecated, @Override and @SuppressWarnings

Annotation 1: @Deprecated 

  • It is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form.
  • The Javadoc @deprecated tag should be used when an element has been deprecated.
  • @deprecated tag is for documentation and @Deprecated annotation is for runtime reflection.
  • @deprecated tag has higher priority than @Deprecated annotation when both are together used.

Annotation 2: @Override

It is a marker annotation that can be used only on methods. A method annotated with @Override must override a method from a superclass. If it doesn’t, a compile-time error will result (see this for example). It is used to ensure that a superclass method is actually overridden, and not simply overloaded.

Annotation 3: @SuppressWarnings 

It is used to inform the compiler to suppress specified compiler warnings. The warnings to suppress are specified by name, in string form. This type of annotation can be applied to any type of declaration.

Java groups warnings under two categories. They are deprecated and unchecked . Any unchecked warning is generated when a legacy code interfaces with a code that uses generics.

Annotation 4: @Documented 

It is a marker interface that tells a tool that an annotation is to be documented. Annotations are not included in ‘Javadoc’ comments. The use of @Documented annotation in the code enables tools like Javadoc to process it and include the annotation type information in the generated document.

Annotation 5: @Target 

It is designed to be used only as an annotation to another annotation. @Target takes one argument, which must be constant from the ElementType enumeration. This argument specifies the type of declarations to which the annotation can be applied. The constants are shown below along with the type of the declaration to which they correspond.

We can specify one or more of these values in a @Target annotation. To specify multiple values, we must specify them within a braces-delimited list. For example, to specify that an annotation applies only to fields and local variables, you can use this @Target annotation: @Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE}) @Retention Annotation It determines where and how long the annotation is retent. The 3 values that the @Retention annotation can have:

  • SOURCE: Annotations will be retained at the source level and ignored by the compiler.
  • CLASS: Annotations will be retained at compile-time and ignored by the JVM.
  • RUNTIME: These will be retained at runtime.

Annotation 6: @Inherited 

@Inherited is a marker annotation that can be used only on annotation declaration. It affects only annotations that will be used on class declarations. @Inherited causes the annotation for a superclass to be inherited by a subclass. Therefore, when a request for a specific annotation is made to the subclass, if that annotation is not present in the subclass, then its superclass is checked. If that annotation is present in the superclass, and if it is annotated with @Inherited, then that annotation will be returned. 

Annotation 7: User-defined (Custom) 

User-defined annotations can be used to annotate program elements, i.e. variables, constructors, methods, etc. These annotations can be applied just before the declaration of an element (constructor, method, classes, etc). 

Syntax: Declaration

Do keep these certain points as rules for custom annotations before implementing user-defined annotations. 

  • AnnotationName is an interface.
  • The parameter should not be associated with method declarations and throws clause should not be used with method declaration.
  • Parameters will not have a null value but can have a default value.
  • default value is optional.
  • The return type of method should be either primitive, enum, string, class name, or array of primitive, enum, string, or class name type.

Please Login to comment...

  • java-basics
  • CharchitKapoor
  • solankimayank
  • prakharsinha2k2
  • Dopple.ai Outperforms Leading Social Networks in User Time Spent In-App
  • Replica Studios vs. Descript Overdub: Which AI Generates Better Synthetic Voices?
  • How To Use ChatGPT for Your Job Search (2024)
  • Android Phones Might Get a Secure Face Unlock Upgrade: Introducing PolarID by Metalenz
  • Dev Scripter 2024 - Biggest Technical Writing Event By GeeksforGeeks

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

  • Java Software
  • Java SE Downloads
  • Java SE 8 Documentation

How and When To Deprecate APIs

What "deprecated" means, when to deprecate, how to deprecate, using the @deprecated annotation, using the @deprecated javadoc tag.

You may have heard the term, "self-deprecating humor," or humor that minimizes the speaker's importance. A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it, since it has been superseded and may cease to exist in the future.

Java provides a way to express deprecation because, as a class evolves, its API (application programming interface) inevitably changes: methods are renamed for consistency, new and better methods are added, and fields change. But such changes introduce a problem. You need to keep the old API around until developers make the transition to the new one, but you don't want them to continue programming to the old API.

The ability to deprecate a class, method, or member field solves the problem. Java supports two mechanisms for deprecation: and an annotation, (supported starting with J2SE 5.0) and a Javadoc tag (supported since 1.1). Existing calls to the old API continue to work, but the annotation causes the compiler to issue a warning when it finds references to deprecated program elements. The Javadoc tag and associated comments warn users against using the deprecated item and tell them what to use instead.

When you design an API, carefully consider whether it supersedes an old API. If it does, and you wish to encourage developers (users of the API) to migrate to the new API, then deprecate the old API. Valid reasons to deprecate an API include:

  • It is insecure, buggy, or highly inefficient
  • It is going away in a future release
  • It encourages bad coding practices

Deprecation is a reasonable choice in all these cases because it preserves "backward compatibility" while encouraging developers to change to the new API. Also, the deprecation comments help developers decide when to move to the new API, and so should briefly mention the technical reasons for deprecation.

It is not necessary to deprecate individual member fields (properties) of a deprecated class, unless of course you want to explain a specific point about a property.

Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead.

Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used. The compiler suppresses deprecation warnings if a deprecated item is used within an entity which itself is deprecated or is used within the same outermost class or is used in an entity that is annotated to suppress the warning.

You are strongly recommended to use the Javadoc @deprecated tag with appropriate comments explaining how to use the new API. This ensures developers will have a workable migration path from the old API to the new API. For more information, see Using the @deprecated Javadoc Tag .

NOTE : The Java Language Specification requires compilers to issue warnings when classes, methods, or fields marked with the @Deprecated annotation are used. Compilers are not required by the Java Language Specification to issue warnings when classes, methods, or fields marked with the @deprecated Javadoc tag are accessed, although the Sun compilers currently do so. However, there is no guarantee that the Sun compiler will always issue such warnings.

J2SE 5.0 introduces a new language feature called annotations (also called metadata). One of the Java language's built-in annotations is the @Deprecated annotation. To use it, you simply precede the class, method, or member declaration with "@Deprecated."

Using the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element. In contrast, there is no guarantee that all compilers will always issue warnings based on the @deprecated Javadoc tag, though the Sun compilers currently do so. Other compilers may not issue such warnings. Thus, using the @Deprecated annotation to generate warnings is more portable that relying on the @deprecated Javadoc tag.

In addition, the @Deprecated annotation causes the javadoc-generated documentation to be marked "Deprecated" wherever that program element appears.

NOTE: Deprecation applies to classes and to individual methods or properties, not to their names. It is possible for a single method to have deprecated and non-deprecated overloadings. It is possible for a non-deprecated property to hide or override a deprecated one, which removes deprecation. As developer of an API, it is your responsibility to deprecate overrides of a deprecated method, if in fact they should be deprecated.

The following is a simple example of using the @Deprecated annotation from java.lang.Thread :

You can use the @deprecated tag to make Javadoc show a program element as deprecated. The @deprecated tag must be followed by a space or newline. In the paragraph following the @deprecated tag, explain why the item has been deprecated and suggest what to use instead.

Javadoc generates special HTML based on @deprecated tags: it moves the paragraph following the @deprecated tag to the front of the description, placing it in italics and preceding it with a warning, "Note: foo is deprecated", in bold. It also adds "Deprecated" in bold to any index entries mentioning the deprecated entity.

The tagged paragraph can be empty, but empty deprecation paragraphs are bad form, because they do not help the user fix the warnings that arise from the deprecation. Include paragraphs marked with @link or @see tags that refer to the new versions of the same functionality. It is usually not a good idea to mention a timetable for phase-out of the deprecated API; this is a business decision that is best communicated other ways.

For more information on using the @deprecated Javadoc tag, see Javadoc - The Java API Documentation Generator .

The following examples show how to use the @deprecated Javadoc tag. They also illustrate the @Deprecated annotation, to emphasize that the two should be used together.

Here is an example of the most common form of a deprecated method (for Javadoc 1.2 and later):

If the API reorganization was more than renaming, the deprecation may be more complex. Here is an example of a method that is being retracted:

IMAGES

  1. What is @Deprecated annotation in Java

    what is @deprecated annotation in java

  2. @deprecated in Java

    what is @deprecated annotation in java

  3. What are Built-In Annotations in Java?

    what is @deprecated annotation in java

  4. what is annotation in java with example

    what is @deprecated annotation in java

  5. Annotations in java

    what is @deprecated annotation in java

  6. what is annotation in java with example

    what is @deprecated annotation in java

VIDEO

  1. @Qualifier Annotation in Spring

  2. 21. @Required annotation in Spring

  3. Session 8 Object Initialization in Java

  4. Intro to Java. Unit 6. GUI with Swing. Part 2. Inner Classes. Applets (in Russian)

  5. Why is Thread.stop() deprecated?

  6. @Component Annotation in Spring

COMMENTS

  1. Java @Deprecated Annotation

    2. The @Deprecated Annotation. As a project evolves, its API changes. Over time, there are certain constructors, fields, types or methods that we don't want people to use anymore. Instead of breaking the backward compatibility of the project's API, we can tag these elements with the @Deprecated annotation. @Deprecated tells other developers ...

  2. The @Deprecated Annotation in Java

    The @Deprecated Annotation in Java. The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is. It's no longer relevant. It is so unimportant that you should stop using it because it has been ...

  3. java

    9. This means that the author wants to remove this method, but didn't do this yet to not break backward compatibility. This also means, that you should not use this method, and if your are already using it, you should stop using it. The method could be marked as deprecated because another method exists that supersedes functionality of this ...

  4. How to Deprecate APIs

    Deprecating an API requires using two different mechanisms: the @Deprecated annotation and the @deprecated JavaDoc tag. The @Deprecated annotation marks an API in a way that is recorded in the class file and is available at runtime. This allows various tools, such as javac and jdeprscan, to detect and flag usage of deprecated APIs.

  5. Deprecated (Java Platform SE 8 )

    A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code. Since: 1.5. See The Java™ Language Specification:

  6. Deprecated (Java SE 11 & JDK 11 )

    There is no defined order among annotation elements. As a matter of style, the since element should be placed first. The @Deprecated annotation should always be present if the @deprecated javadoc tag is present, and vice-versa. Since: 1.5 See The Java™ Language Specification: 9.6.4.6 @Deprecated

  7. Java @Deprecated annotation examples

    The deprecated element is also used in another deprecated element, e.g. a deprecated class invokes a deprecated method. The deprecated element is used within an entity which is annotated by the @SuppressWarnings("deprecation") annotation. The deprecated element is declared and used both within the same outermost class. For example:

  8. Annotations

    Some annotation types are used by the Java compiler, and some apply to other annotations. Annotation Types Used by the Java Language. The predefined annotation types defined in java.lang are @Deprecated, @Override, and @SuppressWarnings. @Deprecated @Deprecated annotation indicates that the marked element is deprecated and should no longer be used.

  9. @Deprecated annotation in java

    Output: This method is marked as deprecated. 10. In the above example we have a deprecated method and a deprecated field. As you can see that we have marked both of them using @Deprecated annotation and in the comment section we have used @deprecated javadoc tag (for documentation purpose) to inform the programmer what to use in place of it.

  10. Predefined Annotation Types (The Java™ Tutorials

    The predefined annotation types defined in java.lang are @Deprecated, @Override, and @SuppressWarnings. @Deprecated @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation.

  11. @deprecated in Java

    In Java, @Deprecated is an annotation that helps in informing the compiler that the specific class, method, or field is no longer in use since it is ceased or superseded. Moreover, a warning should be given when anyone tries to use it. The main advantage for deprecation is in the case where any methods are renamed or added, changes occur.

  12. How to declare or mark a Java method as deprecated?

    41. There are two things you can do: Add the @Deprecated annotation to the method, and. Add a @deprecated tag to the javadoc of the method. You should do both! Quoting the java documentation on this subject: Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation.

  13. Java @Deprecated Annotation

    From Oracle Java Doc: A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code. Let me put in a simple way, the @Deprecated annotation is used ...

  14. What is the use of the @Deprecated annotation in Java?

    The @Deprecated annotation in Java is used to indicate that a particular method, class, or field is deprecated and should no longer be used. When a method, class, or field is marked as deprecated, it is a signal to developers that they should avoid using it, as it may be removed in a future release of the API. ...

  15. What is the best way to comment a deprecated class in Java?

    The recommended approach to deprecating a class, method, or field in Java is to use the @Deprecated annotation, which became available in Java 5, or the @deprecated JavaDoc tag, which has been around since Java 1.1. Oracle has a document about the specifics on how and when to deprecate APIs that appears to be relevant.

  16. Should I use JavaDoc deprecation or the annotation in Java?

    Sorted by: 81. You should use both. The Annotation allows the compiler to display a warning whenever a deprecated method is used, and the javadoc explains why. Both are important. As per Oracle's Java Annotations tutorial: When an element is deprecated, it should also be documented using the Javadoc @deprecated tag...

  17. Annotations in Java

    Annotation 1: @Deprecated . It is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form. The Javadoc @deprecated tag should be used when an element has been deprecated. @deprecated tag is for documentation and @Deprecated annotation is for runtime reflection.

  18. Deprecated (Java SE 20 & JDK 20)

    Annotation Interface Deprecated. A program element annotated @Deprecated is one that programmers are discouraged from using. An element may be deprecated for any of several reasons, for example, its usage is likely to lead to errors; it may be changed incompatibly or removed in a future version; it has been superseded by a newer, usually ...

  19. How and When To Deprecate APIs

    Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used. The compiler suppresses deprecation warnings if a deprecated item is used within an entity which itself is deprecated or is used within the same outermost class or is used in an entity that is annotated to suppress the warning ...

  20. annotations

    8. No, you don't need to. Adding the annotation @Deprecated to DeprecatedClass will generate a warning every time it's used. What you should do however, is marking methods in other classes that take your deprecated class as an argument or return it, as deprecated as well. That goes for any access that other code may have to instances of your ...

  21. annotations

    JEP277 define a @Deprecated (EXPERIMENTAL), but it's just a proposition. In hadoop there is InterfaceStability annotation for these purposes. The funny thing is this annotation is not stable. I doubt such thing can appear in JDK. @InterfaceAudience.Public @InterfaceStability.Evolving public class InterfaceStability { /** * Can evolve while ...