The @Deprecated Annotation in Java
Last Updated :
17 Aug, 2022
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?
- Via Deprecated interface
- Via Deprecated class
- Via Deprecating a method
- Via Deprecating a member variable
- 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);
}
}
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();
}
}
OutputThis 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);
}
}
OutputThis 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);
}
}
OutputThis is a deprecated constructor
10
Similar Reads
Java @Documented Annotations
By default, Java annotations are not shown in the documentation created using the Javadoc tool. To ensure that our custom annotations are shown in the documentation, we use @Documented annotation to annotate our custom annotations. @Documented is a meta-annotation (an annotation applied to other ann
2 min read
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.A
9 min read
How to Create Your Own Annotations in Java?
Annotations are a form of metadata that provide information about the program but are not a part of the program itself. An Annotation does not affect the operation of the code they Annotate. Now let us go through different types of java annotations present that are listed as follows: Predefined ann
5 min read
Inherited Annotations in Java
Annotations in Java help associate metadata to the program elements such as classes, instance variables, methods, etc. Annotations can also be used to attach metadata to other annotations. These types of annotations are called meta-annotation. Java, by default, does not allow the custom annotations
5 min read
Servlet with Annotation
As we learned in previous chapters, the Servlet life cycle will be managed by the Servlet container that is under the Web/Application server. Whenever an HttpRequest comes from the client browser, the servlet container will map the request to the corresponding servlet based on the URL mappings provi
8 min read
Customize Java Annotation with Examples
Java annotations are a mechanism for adding metadata information to our source code (Program). They are a powerful part of Java that was added to JDK5. Annotations provide an alternative to the use of XML descriptors. Also, we are able to attach them to packages, classes, interfaces, methods, and fi
3 min read
Java - @Target Annotations
Annotations in java are used to associate metadata to program elements like classes, methods, instance variables, etc. There are mainly three types of annotations in java: Marker Annotation (without any methods), Single-Valued Annotation (with a single method), and Multi-Valued Annotation (with more
3 min read
@SafeVarargs Annotation in Java 9 with Example
@SafeVarargs Annotation: @SafeVarargs annotation is introduced in JDK 7. @SafeVarargs annotation is used to suppress the unsafe operation warnings at the compile time. Unsafe operation warnings come at the compile time whenever in our code we invoked the method which is having varargs i.e. variable
4 min read
Hibernate - Annotations
Annotation in JAVA is used to represent supplemental information. As you have seen @override, @inherited, etc are an example of annotations in general Java language. For deep dive please refer to Annotations in Java. In this article, we will discuss annotations referred to hibernate. So, the motive
7 min read
Hibernate - @Version Annotation with Example
@Version annotation is used to specify the version number for a specific entity. Version Number provided using @Version annotation is used to prevent concurrent modification to an entity. When an entity is being updated, the version number is also incremented. If another transaction tries to update
2 min read