Java Reference
In-Depth Information
Commonly Used Standard Annotations
Java API defines many standard annotation types. This section discusses four of the most commonly used standard
annotations. They are defined in the java.lang package. They are
Deprecated
Override
SuppressWarnings
FunctionalInterface
The Deprecated Annotation Type
The deprecated annotation type is a marker annotation type. Developers are discouraged from using a program
element annotated with a Deprecated annotation because it is not safe to use the program element anymore or
a better alternative exists. If you use a deprecated program element in a non-deprecated code, the compiler will
generate a warning. Suppose you have a DeprecatedTest class as follows. Note the annotation of the class with a
@Deprecated annotation. Its getInstance() method uses the class type as its return type, which will not generate
a compiler warning because it is inside the deprecated class.
Listing 1-16. An Example of Deprecating a Class Named DeprecatedTest
// DeprecatedTest.java
package com.jdojo.annotation;
@Deprecated
public class DeprecatedTest {
private DeprecatedTest() {
}
public static DeprecatedTest getInstance() {
// Using the deprecated class inside its own body
DeprecatedTest dt = new DeprecatedTest();
return dt;
}
}
Let's attempt to use the DeprecatedTest class inside a new class called Test , as follows. When you compile the
Test class, it will generate a compiler note stating that the deprecated DeprecatedTest class should not be used.
package com.jdojo.annotation;
public class Test {
public static void main(String[] args) {
DeprecatedTest dt; // Generates a compile-time note
}
}
Note Test.java uses or overrides a deprecated API.
Note Recompile with -Xlint:deprecation for details.
 
Search WWH ::




Custom Search