Omkar Omkar - 22 days ago 7
Java Question

No compilation warning with @Deprecated annotation?

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();
}


}

Answer

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

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

enter image description here