1. Meta-annotation
1.1 @Target
[Function]
is used to specify the location where the marked annotation can be used, for example: @Target(ElementType.METHOD)
: Indicates that it can be used on methods, but other structures cannot be used; @Target({ElementType .METHOD, ElementType.TYPE})
: Indicates that it can be used on methods, interfaces, classes, and enumerations.
Related online video tutorials: java course
1.2 @Retention
[Function]
Use In the designated annotation retention stage, the annotation has three values:
@Retention(RetentionPolicy.SOURCE)
: indicates that it is retained until the source code stage and disappears after compilation
@Retention(RetentionPolicy.CLASS)
: Retains until the compilation phase and disappears after running
@Retention(RetentionPolicy.RUNTIME)
: Retains until the running phase , if you want to read the annotation information through reflection, you need to specify the annotation retention stage as RUNTIME
1.3 @Inherited
[Function]
indicates this Whether the annotation can be inherited by subclasses.
1.4 @Documented
[Function]
Indicates whether this annotation can be read into the document by Javadoc.
2. Annotation statement
[Format]
[Meta-annotation]
【修饰符】 @interface 注解名 { 注解体 }
[Example]
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { }
3. Configuration parameter declaration
[Format]
【数据类型】 参数名() default 默认值;
default Default value: When you need to set the default value, you can add it. When you need to set it, you don’t need to write it;
Data types can only be: basic data types, String, Class, enum, Annotation, and one-dimensional arrays of all the above types.
If there is only one parameter member or a frequently used parameter, you can define the parameter name: value. When using annotations, if the parameter name is value, you can omit it and write the input value directly.
[Example]
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String name() default ""; }
4. Read annotation information
Only annotation annotations@Retention(RetentionPolicy.RUNTIME)
It can be read through reflection.
Read annotation information through reflection, as follows:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class TestInterface { public static void main(String[] args) { MyAnnotation myAnnotation = MyClass.class.getAnnotation(MyAnnotation.class); String value = myAnnotation.value(); System.out.println(value); } } @MyAnnotation class MyClass {} @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String value() default "我是一个注解"; }
Output results:
Recommended related article tutorials :java introductory learning
The above is the detailed content of Annotations for java basics. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.