BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

@Deprecated annotation in java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

@Deprecated annotation is used for informing compiler that the particular method, class or field is deprecated and it should generate a warning when someone try to use any of the them.

What is the meaning of “Deprecated”?

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, in such case you can deprecate the particular item using built-in annotation. Source.

How to Deprecate?

We deprecate a method, class or field by using @Deprecated annotation and we use @deprecated Javadoc tag in the comment section to inform the developer, the reason of deprecation and what can be used in place of this deprecated method, class or field. Lets take an example:

Example

class DeprecatedDemo {
   /* @deprecated This field is replaced by 
    * MAX_NUM field
    */
   @Deprecated
   int num=10;
	
   final int MAX_NUM=10;
    
   /* @deprecated As of release 1.5, replaced 
    * by myMsg2(String msg, String msg2)
    */
   @Deprecated
   public void myMsg(){
       System.out.println("This method is marked as deprecated");
   }
     
   public void myMsg2(String msg, String msg2){
       System.out.println(msg+msg2);
   }
    
   public static void main(String a[]){      
    	DeprecatedDemo obj = new DeprecatedDemo();
        obj.myMsg();
        System.out.println(obj.num);
   }
}

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.

Note: When you use deprecated types(method, class or field) the compiler would not throw a compilation error it would just show a warning to let you know that this is deprecated and you may have a better alternative of it, which you can find out in the comments section by looking for @deprecated tag.

Reference:
How and When To Deprecate APIs

Top Related Articles:

  1. Java Enum Tutorial with examples
  2. Java Date – Convert 12 hour format to 24 hour format and vice versa
  3. Sorting float array in Java example
  4. Java 9 – Anonymous Inner classes and Diamond Operator
  5. OOPs concepts – What is Aggregation in java?

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap