Omkar Shetkar
Omkar Shetkar

Reputation: 3636

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

}

Upvotes: 5

Views: 5163

Answers (2)

Nishant
Nishant

Reputation: 1

For Compiling and running your Java program using Command Prompt:

javac -Xlint className.java

Screenshot

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

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

enter image description here

Upvotes: 4

Related Questions