Home  >  Article  >  Java  >  Detailed explanation and examples of @interface annotations in Java

Detailed explanation and examples of @interface annotations in Java

黄舟
黄舟Original
2017-06-04 09:31:311728browse

This article mainly introduces the relevant information of java @interface annotation and examples. Friends who need it can refer to it

java @interface annotation details and examples

1 Introduction

In Java, defining annotations is actually similar to defining interface. You only need to add an @ symbol before the interface. That's it, that is, @interface Zhujie{ }, which indicates that we have defined an annotation named @Zhujie. Each method in the annotation defines an element of the annotation type. Special attention: the declaration of the method in the annotation must not contain parameters or throw an exception; the return value of the method is limited to simple types, String, Class, emnus, annotations, and arrays of these types, but methods can have a default value.

Annotations are equivalent to a kind of mark. Adding annotations to a program is equivalent to adding some kind of mark to the program. JAVAC compilers, development tools and other programs can use the reflection mechanism to understand our classes and Whether there are tags on various elements, if the tag is found, do the corresponding thing. For example, @Deprecated can be marked on some classes, methods and fields that are not recommended for use, and a warning will be given if someone uses them.

2 Meta-annotation

Annotation @Retention can be used to modify annotations. It is an annotation of annotations and is called a meta-annotation. The Retention annotation has a attribute value, which is of type RetentionPolicy, and Enum RetentionPolicy is an enumeration type, which determines how the Retention annotation should be operated. It can also be understood as using Rentention with RententionPolicy. RetentionPolicy has three values: CLASS, RUNTIME and SOURCE.

  1. Annotations modified with @Retention(RetentionPolicy.CLASS) indicate that the annotation information is retained in the class file (bytecode file) when the program is compiled, but will not be virtualized Machine reading is running;

  2. The annotation modified with @Retention(RetentionPolicy.SOURCE) means that the annotation information will be discarded by the compiler and will not be left in the class file. The annotated information will only remain in the source file;

  3. The annotation modified with @Retention(RetentionPolicy.RUNTIME) indicates that the annotated information is retained in the class file (bytecode file) , when the program is compiled, it will be retained by the virtual machine at runtime.

3 Usage example

First, create a simple annotation:

public @interface Coder { 
   int personId(); 
   String company() default "[unassigned]";
}

After the annotation is defined, we can use it to make annotation statements. Annotations are a special modifier that can be used wherever other modifiers (for example, public, static, or final, etc.) are used. By convention, annotations should be placed before other modifiers. The annotation is declared with the @ symbol followed by the name of the annotation type, followed by parentheses. The key-value pairs of the elements or methods in the annotation are listed in the parentheses, where the value must be constant. For example:

@coder(personId=20151120,company="YeePay")

Annotations without elements or methods are called "marker" types. For example:

public @interface Coder {}

When a marker annotation is used, the brackets following it can be omitted. If the annotation contains only one element, the name of the element should be value, for example:

public @interface Coder { 
   String value();
}

If the name of the element is value, then when using this annotation, the name of the element and the equal sign can be omitted. For example:

@Coder("YeePay")

The above is the detailed content of Detailed explanation and examples of @interface annotations in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn