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
public class GFG {
@Deprecated
int number = 100 ;
final int newnumber = 100 ;
public static void main(String a[])
{
GFG obj = new GFG();
System.out.println(obj.number);
}
}
|
Example 2: Using deprecated method name.
Java
public class GFG {
@Deprecated
public void oldmethod()
{
System.out.println( "This is a deprecated method" );
}
public void newmethod(String m1)
{
System.out.println(m1);
}
public static void main(String a[])
{
GFG obj = new GFG();
obj.oldmethod();
}
}
|
Output
This is a deprecated method
Example 3: Using deprecated method name as well as the deprecated variable name.
Java
class GFG {
@Deprecated
int no = 10 ;
final int MAX_NUM = 100 ;
@Deprecated
public void gfgMsg()
{
System.out.println( "This method is deprecated" );
}
public void gfgMsg2(String msg, String msg2)
{
System.out.println(msg + msg2);
}
public static void main(String a[])
{
GFG obj = new GFG();
obj.gfgMsg();
System.out.println(obj.no);
}
}
|
Output
This method is deprecated
10
Example 4: Using deprecated constructor as well as the deprecated variable name.
Java
public class GFG {
@Deprecated
int number = 10 ;
final static int newnumber = 10 ;
GFG( int a, int b, int c)
{
System.out.println(
"This is a deprecated constructor" );
}
GFG( float d, int e, float f)
{
System.out.println(d + f);
}
public static void main(String a[])
{
GFG obj = new GFG(newnumber, newnumber, newnumber);
System.out.println(obj.number);
}
}
|
Output
This is a deprecated constructor
10
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...