Three common annotations (annotations) in Java Annotation

Source: Internet
Author: User

Java provides us with three kinds of annotation to facilitate our development.

1 override-function Overwrite annotations

If we want to overwrite the ToString () method of object, take a look at the following code:

1 classAnnotationdemo2 {3     PrivateString info;4      PublicAnnotationdemo (String info)5     {6          This. info =info;7     }8     9      PublicString ToString ()Ten     { One         returnThe value of info is: "+ This. info; A     } - } -  the  Public classMain - { -      Public Static voidMain (string[] args) -     {  +Annotationdemo AD =NewAnnotationdemo ("Hello"); - System.out.println (AD); +System.out.println ("Main done//~~"); A     }  at}

The above code, we expect to be able to overwrite the ToString () method in the Annotationdemo class, the result is run, it is found that the program is called the ToString method of object. The reason is that our function code is flawed, and the method name that should have been ToString () is written as ToString (). The bug will not be exposed until we run the code. If we add @override annotations to ToString (), we can show the Java compiler that my function is to overwrite the parent class method, and then perform the check. Please look at the code:

1  Packagemain;2  3 4 classAnnotationdemo5 {6     PrivateString info;7      PublicAnnotationdemo (String info)8     {9          This. info =info;Ten     } One      A @Override -      PublicString ToString () -     { the         returnThe value of info is: "+ This. info; -     } - } -  +  Public classMain - { +      Public Static voidMain (string[] args) A     {  atAnnotationdemo AD =NewAnnotationdemo ("Hello"); - System.out.println (AD); -System.out.println ("Main done//~~"); -     }  -}

The above code cannot be compiled.

2 Depreced-Method Overdue annotations

If we use @depreced annotations on the method, we are telling the user that this method is deprecated. As in the following code:

1 classAnnotationdemo2 {3     PrivateString info;4      PublicAnnotationdemo (String info)5     {6          This. info =info;7     }8     9 @DeprecatedTen      Public voidShowinfo () One     { ASystem.out.println ( This. info); -     } -      the @Override -      PublicString toString () -     { -         returnThe value of info is: "+ This. info; +     } - } +  A  Public classMain at { -      Public Static voidMain (string[] args) -     {  -Annotationdemo AD =NewAnnotationdemo ("Hello"); - System.out.println (AD); -System.out.println ("Main done//~~"); in     }  -}

The above code warns the user when compiling, and the Showinfo () method is deprecated.

3 @SuppressWarning-Suppress warning

Suppressing the warning means that when our code has a warning message, and we don't think the warning will pose a threat to our code, we can use @suppresswarning to cancel the warning message.

1@SuppressWarnings ("Serial")2 classAnnotationdemoImplementsSerializable3 {4     PrivateString info;5      PublicAnnotationdemo (String info)6     {7          This. info =info;8     }9     Ten @Deprecated One      Public voidShowinfo () A     { -System.out.println ( This. info); -     } the      - @Override -      PublicString toString () -     { +         returnThe value of info is: "+ This. info; -     } +}

The above code implements the serializable interface, which requires that a SERIALVERSIONUID field in the class has a different version of the flag. In fact, we do not need this field, then the warning is suppressed on the class, the compiler will not prompt the warning message.

Three common annotations (annotations) in Java Annotation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.