The basic Annotation
- @Override
- @Deprecated
- @SuppressWarnings
- @SafeVarargs
Override specifies method overrides that force a subclass to Override methods of its parent class.
- The main purpose is to help us avoid some low-level errors, such as: there is a run() method in the parent class, the method name of the child class is accidentally written as runn(), the program will be compiled at the time of error.
- Applies only to methods, not to other program elements.
Deprecated is used to indicate that an element (class, method, etc.) is Deprecated. The compiler will warn other programs that use Deprecated classes or methods.
- Although there is a warning, but does not affect the use, use can still be used
// Parent class, where methods labeled test() are obsolete. Public class SupperClass {@deprecated public void test(){system.out.println (" I am the parent of the obsolete method test"); }} // If the subclass calls yes, the compiler will warn that the method is obsolete. public class SonClass extends SupperClass{ public static void main(String[] args) { SonClass sonClass = new SonClass();Copy the code
sonClass. test() ; // The prompt method is out of date
}}Copy the code
@SuppressWarnings instructs the program element decorated by this Annotation (and all children of the program) to undisplay the specified compiler warning.
- If a method with the @deprecated annotation is used, the compiler will display a warning message where the method is called. Putting @SuppressWarnings(“deprecation”) on the class that called the outdated method will remove the warning message.
Public class SupperClass {@deprecated public void test(){system.out.println (" I am the parent of the obsolete method test"); }} @suppressWarnings ("deprecation")// With this annotation, Public class SonClass extends SupperClass{public static void main(String[] args) {SonClass SonClass = new SonClass(); sonClass.test(); }}Copy the code
The Java compiler will emit an unchecked warning when declaring constructors or methods that have mutable arguments of obscure types, such as generics.
- Such situations can be marked with @safevarargs if you determine that declared constructors and method bodies will not perform potentially unsafe operations on their VARargs arguments. In this way, the Java compiler will not emit an unchecked warning.
- Methods that are not static or final declarations do not apply and will fail to compile. To SuppressWarnings, use the @suppresswarnings annotation: @suppresswarnings (“unchecked”)