Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the three standard annotations and four meta annotations in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

What are the three standard notes and four meta-annotations in Java? most people do not understand the knowledge points of this article, so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the three standard notes and four meta-annotations in Java".

Three standard annotations and four meta annotations of Java let's first talk about what annotations are.

Annotations, also known as metadata, provide a formal way for us to add information to our code, making it very convenient for us to use the data at a later time.

The syntax of annotations is relatively simple, and except for the use of the @ symbol, it is basically consistent with the inherent syntax of Java.

Java has three built-in annotations, the annotations defined in java.lang

Override: indicates that the current method definition will override the methods in the superclass. If you accidentally make a spelling mistake, or if the method signature does not match the overwritten method, the compiler will issue an error prompt.

Deprecated: if the programmer uses an element annotated to it, the compiler issues a warning message.

SuppressWarnings: turn off improper compiler warning messages. In previous versions of java SE5, this annotation could also be used, but it would not work if it was ignored.

Let's take a look at the definition of the annotation @ Test:

When defining annotations, you need some meta-annotations (meta-annotation), such as @ Target and @ Retention.

Target is used to define where your annotations will be applied (for example, a method or a domain).

@ Retention is used to define the level at which the annotation is available, in source code (SOURCE), class file (CLASS), or run time (RUNTIME).

Java has four meta annotations as well as three built-in standard annotations

@ Target indicates where the annotation is used. Possible values are in the enumeration class ElemenetType, including:

ElemenetType.CONSTRUCTOR constructor declaration

ElemenetType.FIELD domain declaration (including enum instances)

ElemenetType.LOCAL_VARIABLE local variable declaration

ElemenetType.METHOD method declaration

ElemenetType.PACKAGE package declaration

ElemenetType.PARAMETER parameter declaration

ElemenetType.TYPE class, interface (including annotation type) or enum declaration

@ Retention indicates at what level the annotation information is saved. The optional parameter values are in the enumeration type RetentionPolicy, including:

RetentionPolicy.SOURCE comments will be discarded by the compiler

RetentionPolicy.CLASS annotations are available in class files, but are discarded by VM

RetentionPolicy.RUNTIME VM will also retain comments at run time, so the annotated information can be read through the reflection mechanism.

Documented includes this annotation in javadoc, which means that it will be extracted into documentation by the javadoc tool. The content in the doc document will vary depending on the information content of this comment. Equivalent to @ see,@param and so on.

@ Inherited allows subclasses to inherit annotations in the parent class.

Meta-annotation and write a meta-annotation yourself

@ Target indicates where this annotation can be used

@ Retention indicates where our comments are still valid (runtime > class > sources)

@ Documented indicates whether our annotations will be generated in javaDoc

@ Inherited subclass can inherit comments from the parent class

Package com.epoch.customproject.utils;import java.lang.annotation.*;@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) @ Documentedpublic @ interface DataSource {String value () default "dataSourceMdm" Write your own annotation public class test {@ Myobject2 (age = 18) public void test1 () {} @ Myobject () {} @ Target (value = {ElementType.TYPE,ElementType.METHOD}) @ Retention (value = RetentionPolicy.RUNTIME) @ interface Myobject {/ / there is only one parameter, so just pass a value @ Myobject ("default") String value () } @ Target (value = {ElementType.TYPE,ElementType.METHOD}) @ Retention (value = RetentionPolicy.RUNTIME) @ interface Myobject2 {/ / @ Myobject2 (age = 18) the reason why other parameters do not need to be passed is that the parameter type parameter name () String name () default with the default value / / comment is given "default name"; int age (); double height () parameter-1 / / if the default value is-1, it means that there is no String [] school () default {"default school 1", "default school 2"};} above is the content of this article on "what are the three standard notes and four meta-annotations in Java?" I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report