According to Java tutorial on Oracle, if deprecated method marked with @Deprecated annotation is used, compiler should be giving warning on compilation. But with following code sample, I am not getting any warning in the console.
Java version used: 1.8.0_112
Please let me know what could be missing here. Thanks.
public class PreDefinedAnnotationTypesTest {
/**
* This method is deprecated.
* @deprecated
*/
@Deprecated
public void m1(){
}
public static void main(String[] args) {
PreDefinedAnnotationTypesTest obj = new PreDefinedAnnotationTypesTest();
obj.m1();
}
}
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.
Using the @Deprecated Annotation J2SE 5.0 introduces a new language feature called annotations (also called metadata). One of the Java language's built-in annotations is the @Deprecated annotation. To use it, you simply precede the class, method, or member declaration with "@Deprecated."
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
You can use the @SuppressWarnings annotation to suppress warnings whenever that code is compiled. Place the @SuppressWarnings annotation at the declaration of the class, method, field, or local variable that uses a deprecated API.
From docs
The compiler suppresses deprecation warnings if a deprecated item is used within an entity which itself is deprecated or
is used within the same outermost class
or is used in an entity that is annotated to suppress the warning.
so your function is being used within the same class in which it is declared simply try to use in some other class.
In the below image the wontShowWarning
function will not generate any warning although show()
funtion will, which is from another class.
The API design can have different rules for itself because it is presumed that the outermost classes will be modified according to new design so this is just a indication to other classes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With