@Deprecated
Marks an outdated class, method or field. The compiler warns when they are used.
Available since: Java 5
Syntax
@Documented @Retention(RUNTIME) public @interface DeprecatedParameters
| Parameter | Type | Description |
|---|---|---|
| since | String | The version in which the element became deprecated (Java 9). |
| forRemoval | boolean | true means the element will be removed in a future version (Java 9). |
Examples
A deprecated method still works, and the annotation is available at run time:
class Calculator {
@Deprecated(since = "2.0", forRemoval = true)
int add(int a, int b) {
return sum(a, b);
}
int sum(int a, int b) {
return a + b;
}
}
void main() throws Exception {
IO.println(new Calculator().add(2, 3));
Deprecated info = Calculator.class
.getDeclaredMethod("add", int.class, int.class)
.getAnnotation(Deprecated.class);
IO.println(info.since() + " " + info.forRemoval());
}Output:
5
2.0 trueUsing a deprecated JDK API — the compiler prints a warning, but the program still runs:
void main() {
Integer n = new Integer(5);
IO.println(n);
}Output:
Main.java:2: warning: [deprecation] Integer(int) in Integer has been deprecated
Integer n = new Integer(5);
^
1 warning
5Notes
Along with the annotation, the Javadoc gets a @deprecated tag explaining what to use instead. The warning appears only when the element is used from another top-level class; inside the same file class there is none. Warnings are turned off with @SuppressWarnings("deprecation"), and for forRemoval = true with @SuppressWarnings("removal").
See also
- @SuppressWarnings — Turns off chosen compiler warnings for a class, method or variable.
- @Override — Marks a method that overrides a method of a parent class or interface. The compiler checks that it really does.
- Object.getClass() — Returns the runtime class of an object.
In the tutorial
- Annotations — Annotations in Java — @Override, @Deprecated, @FunctionalInterface, @SuppressWarnings, declaring your own annotation, @Retention, @Target and reading annotations at run time.