Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaThe @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 superseded and may be phased out in the future.

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. Because a class’s API (application programming interface) changes over time, Java provides a way to express deprecation. Methods are renamed for consistency, new and better methods are added, and fields change. However, such changes pose a problem. If you need to keep the old API around until developers transition to the new one but don’t want them to continue programming against it, you can use the built-in annotation to deprecate the item.

Now the use of @Deprecated annotation is shown as follows: The API of a project evolves over time. We don’t want people to use certain constructors, fields, types, or methods any longer as time goes on. Instead of breaking the project’s API’s backward compatibility, we can use the @Deprecated annotation to mark these elements as deprecated. @Deprecated indicates to other developers that the marked element should be avoided.

With Java 9, two new enhancements are made to @Deprecated annotation:

  • forRemoval: Indicates whether the annotated element is subject to removal in a future version. The default value is false.
  • since: Returns the version in which the annotated element became deprecated. The default value is the empty string.

How to Deprecate?

  1. Via Deprecated interface
  2. Via Deprecated class
  3. Via Deprecating a method
  4. Via Deprecating a member variable
  5. Via Deprecating a constructor

We use the @Deprecated annotation to deprecate a method, class, or field, and the @deprecated Javadoc tag in the comment section to inform the developer about the reason for deprecation and what can be used in its place.

1. Deprecated interface:

@Deprecated

interface GFG {
  // Interface methods
}

2. Deprecated class

@Deprecated

class GFG {
// Class implementation
}

3. Deprecating a method

class GFG {

  @Deprecated

  // old method
  public void gfgmethod() {}

  // new, alternate method
  public void gfgmethod2() {}
}

4. Deprecating a member variable

class GFG {
  
  @Deprecated
  
  public static final int MAX_SIZE  = 1024;
  // new, alternate field
  public static final int MAX_UPLOAD_SIZE = 1024;
}

5. Deprecating a constructor

class GFG {

  @Deprecated
  
  Gfg(String name, int length, int width) {

  }
  
  // new, alternate constructor
  Gfg(Style style) {

  }
}

Implementation: Now let us do implement them out with a clean java program for the same.

Example 1: Using deprecated variable name.

Java




// Java Program Illustrating The @Deprecated Annotation
// Using deprecated variable name
 
// Main class
public class GFG {
 
    // @deprecated number1 will be replaced by
    // newnum field
    @Deprecated
 
    // Declaring and initializing integer variables
    int number = 100;
    // New field
    final int newnumber = 100;
 
    // Main
    public static void main(String a[])
    {
        // Creating an object for the class
        GFG obj = new GFG();
 
        // Printing the number
        System.out.println(obj.number);
    }
}


Output

100

Example 2: Using deprecated method name.

Java




// Java Program Illustrating The @Deprecated Annotation
// Using deprecated method name
 
// Main class
public class GFG {
 
    // @deprecated The function oldmethod will be replaced
    //  by new method
    @Deprecated
 
    // Method 1
    // Old method
    public void oldmethod()
    {
 
        // Print statement
        System.out.println("This is a deprecated method");
    }
 
    // Method 2
    // New method
    public void newmethod(String m1)
    {
 
        // Print statement
        System.out.println(m1);
    }
 
    // Method 3
    // Main driver method
    public static void main(String a[])
    {
 
        // Creating an object of class
        GFG obj = new GFG();
 
        // Now calling the old method
        obj.oldmethod();
    }
}


Output

This is a deprecated method

Example 3: Using deprecated method name as well as the deprecated variable name.

Java




// Java Program Illustrating The @Deprecated Annotation
// Using deprecated method name
// as well as the deprecated variable name
 
// Main class
class GFG {
 
    // @deprecated
    // The old function will be replaced by new one
    @Deprecated
 
    // Declaring and initializing integer values
    int no = 10;
    final int MAX_NUM = 100;
 
    @Deprecated
 
    // Method 1
    // Old method
    public void gfgMsg()
    {
 
        // Print statement
        System.out.println("This method is deprecated");
    }
 
    // Method 2
    // New Method
    public void gfgMsg2(String msg, String msg2)
    {
 
        // Print statement
        System.out.println(msg + msg2);
    }
 
    // Method 3
    // Main driver method
    public static void main(String a[])
    {
 
        // Creating an object of class
        GFG obj = new GFG();
 
        // Now calling the old method
        obj.gfgMsg();
 
        // Printing the num
        System.out.println(obj.no);
    }
}


Output

This method is deprecated
10

Example 4: Using deprecated constructor as well as the deprecated variable name.

Java




// Java Program Illustrating The @Deprecated Annotation
// Using deprecated constructor
// as well as the deprecated variable name.
 
// Main class
public class GFG {
 
    // @deprecated
    // The field number will be replaced by newnumber field
    @Deprecated
 
    int number = 10;
 
    // new field
    final static int newnumber = 10;
 
    // @deprecated
    // The constructor depexamplewill be replaced by second
    // depexample
 
    // Old constructor
    GFG(int a, int b, int c)
    {
 
        // Print statement for old constructor
        System.out.println(
            "This is a deprecated constructor");
    }
 
    // new constructor
    GFG(float d, int e, float f)
    {
 
        // Print statement for new constructor
        System.out.println(d + f);
    }
 
    // Main driver method
    public static void main(String a[])
    {
 
        // Creating object of class
        GFG obj = new GFG(newnumber, newnumber, newnumber);
 
        // Print and display the number
        System.out.println(obj.number);
    }
}


Output

This is a deprecated constructor
10
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments