Annotation (1) in java -- three basic Annotation

Source: Internet
Author: User
Introduction to Annotation

Since jdk5.0, Java has added support for metadata (metadata), that is, annotation (annotation, also called annotation). In fact, it is a special mark in the code, these tags can be read during compilation, class loading, and running, and processed accordingly. By using annotation, program developers can embed supplementary information in the source file without changing the original logic. Code analysis tools, development tools, and deployment tools can be verified or deployed through these supplemental information.

Annotation provides a method to set metadata for program elements. In some respects, annotation is used just like a modifier, it can be used to modify the declaration of packages, classes, constructors, methods, member variables, parameters, and local variables. Such information is stored in the "name = value" pair of annotation. Annotation is an interface. The program can obtain the annotation object of the specified program element through reflection and then obtain the metadata in the comment through the annotation object.

Annotation can be used to set metadata for program elements (such as classes, methods, and member variables. It is worth noting that annotation does not affect program code execution. No matter how much annotation is added or deleted, the code is always executed. If you want to enable annotation in the program to play a certain role at runtime, you can only access and process the information in Annotation using a supporting tool, tools for accessing and processing annotation are collectively referred to as apt (annotationprocessing tool ).

Java provides three basic annotation restrictions to override the parent class method: @ override

@ Override is used to specify method rewriting. It can emphasize that a subclass must overwrite the parent class method. In the following program, use @ override to specify the info method of the subclass apple. The parent method must be overwritten.

Public Class fruit {public void Info () {system. Out. println ("fruit info method... ") ;}} Class Apple extends fruit {// use @ override to specify that the following method must override the parent class method @ override // public void Info () Public void inf0 () {system. out. println ("Apple rewrite the info method of fruit... ");}}

If the override method name and parameters are consistent with the parent class, the function of @ override is unknown. However, if the override method is inconsistent with the parent class method, the program will encounter errors during the compilation phase. That is to say, the function of this annotation is to help us avoid some low-level errors.

Mark expired: @ deprecated

@ Deprecated indicates that a program element (class, method, etc.) is outdated. When other programs use outdated classes and methods, the compiler will give a warning. The following Program specifies that the info method in the Apple class is out of date. When other programs use the info method of the apple class, the compiler will give a warning.

Class Apple {// The Info method defined is outdated @ deprecatedpublic void Info () {system. out. println ("Apple info method") ;}} public class deprecatedtest {public static void main (string [] ARGs) {// when the info method is used below, the compiler will warn new Apple (). info ();}}

Methods marked with this annotation in eclipse are marked out by strikethrough to attract the attention of programmers.

Suppress compiler warning: @ suppresswarnings

@ Suppresswarnings indicates that the program elements identified by annotation (and all child elements in the program element) Cancel displaying the specified compiler warning. @ Suppresswarnings always acts on all child elements of the program element. For example, a class identified by @ suppresswarnings is used to cancel displaying a compiler warning, at the same time, it identifies a method in this class to cancel displaying another compiler warning. In this method, both compiler warnings are canceled.

In general, if a set without generic restrictions is used in the program, it will cause a compiler warning. To avoid such a compiler warning, you can use the annotation @ suppresswarnings, the following program cancels the warning that no generic compiler is used.

@SuppressWarnings(value="unchecked")public class SuppressWarningsTest{public static void main(String[] args) {List<String> myList = new ArrayList();}}

As shown in the program, when we use this annotation to disable the compiler warning, we must use the name = value pair in brackets to set the value for the annotation member variable.

The above is a brief introduction to annotations. The next blog will introduce the working principle of annotation in detail.

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.