10 minutes to learn Java annotations (annotation)

Source: Internet
Author: User

(original)

recognize annotations First ( Annotation)

Define classes with class, define interfaces with interface, define annotations with @interface

as  Public @interface annotationtest{}

all annotation types are automatically inherited from the Java.lang.annotation.Annotation interface

This is described in the Annotation interface documentation: All annotation types are extended to this common interface, note that an interface,

The manual inheritance of this interface does not imply that it is an annotation type. In other words, the interface itself is not an annotation type.

Note: here Annotation is an interface, not an annotation, and annotations cannot be inherited.

@Retention: Keep Annotations

There's a Retentionpolicy . Enumeration properties for a retention policy

Here are three values:

SOURCE: annotations are discarded by the compiler and are not compiled into a . class file

Class: at compile time, annotations are retained in the . Class compilation file, but are not read by the virtual machine, which is a preset value.

Runtime: annotations are retained in the . Class compilation file and can be acquired by the virtual machine at run time, so they can be read in a reflective manner.

For example: When compiling code, it is used to suppress annotations for IDE warnings @SuppressWarnings and

The overridden annotation @Override

be annotated as SOURCE, these two annotations will only run at compile time and will not be compiled into a . class file

If the annotation requires reflection, it is necessary to understand Annotatedelement

(The above implementation is 1.6 version of the JDK,the1.8 version has been changed, added a few, here is not listed.) )

Note: If you need to use reflection, be sure to define the value of the @Retention as RUNTIME, because his default value is clss.

The following example to understand how to use the annotations, the annotations are written very clearly, there is not much to say.

A note is defined here @AnnotationTest

Another important note is described below

@Target

As with @Retention , there is also a property of an enumeration type

There are a lot of values here, such as method, this value can only be modified,FIELD, this value can only modify properties, etc.

For example, I add a @Target ({Elementtype.field}) to the annotations I wrote myself just now.

Then this place is going to be an error.

Note that this annotation is not allowed to be placed in this place.

There are many JAVA annotations, the actual development of the very few will write their own, are used by the third party or framework, as long as the basic knowledge of its fundamentals, these things are very simple.

10 minutes to learn Java annotations (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.