Table of Contents
1. What is annotation
2. Implementation
In short, an annotation is a mark used to mark code. We can implement different annotation methods by scanning different annotations. Through Java's dynamic proxy and reflection, we can Easily obtain the content of our annotation tags to operate the classes or methods we have written. In the next article, I will define a custom annotation and write its implementation.
Home Java javaTutorial An article to help you understand annotations in Java

An article to help you understand annotations in Java

Jul 23, 2018 am 10:45 AM
java annotation

Annotations are something that anyone who is a Java developer will be familiar with, but we use so many annotations, how do our annotations work for us? Through my study some time ago, I have a new understanding of annotations.

1. What is annotation

In our popular view, annotation is an implementation of the Annotation interface. It is at the same declaration and usage level as classes and interfaces. They all inherit the Object base class and have the .class attribute.

But do the annotations themselves really work?

If you don’t believe it, try defining an annotation yourself and then put it in your code. The result will be useless.

We can think of annotation as a mark with attributes. When we mark our code with this mark, it means that our code has certain characteristics represented by the annotation, but it does not mean that we It has this characteristic the moment I annotate it.

Our code needs to be compiled before running. Sometimes we also need to compile dynamically during runtime. At this time, if we embed reflection or dynamic proxy code to parse this class , and add the characteristics it should have to this class. At this time, the class has the meaning represented by the annotation.

For example, when we were in kindergarten, we needed to raise our hands to go to the toilet, and the teacher would lead us there. At this time, raising your hands means that you have marked the annotation for going to the toilet. If the teacher does not If I were to blame you, you wouldn't be able to go to the toilet and would have to hold it in. If the teacher scans the whole class at this time and finds you raising your hands, she will go to your place and take you with a group of classmates who are all raising their hands. To the restroom. Only then did you develop your ability to go to the toilet.

2. Implementation

Most of the annotations we see are not actually defined by Java at the beginning. The annotations specified at the beginning are only the first four meta-annotations. , they are:

  • @Documented – Whether the annotation will be included in JavaDoc

  • @Retention – When to use the annotation

  • @Target – where the annotation is used

  • @Inherited – whether subclasses are allowed to inherit the annotation

@Documented, this annotation means whether to put the description of this class or method in our java document when we generate javaDoc. Generally, it is useless if you don't use the project documentation tool that comes with Java to generate documents.

@Retention, this annotation represents the life cycle of the annotation we define. Here are its various assignments and descriptions of its functions:

  1. RetentionPolicy.SOURCE: During the compilation phase throw away. These annotations no longer have any meaning after compilation, so they are not written into the bytecode. @Override, @SuppressWarnings all belong to this type of annotations.

  2. RetentionPolicy.CLASS: Discarded when the class is loaded. Useful in processing bytecode files. Annotations use this method by default

  3. RetentionPolicy.RUNTIME: It will never be discarded, and the annotation is retained during runtime, so the reflection mechanism can be used to read the annotation information. Our custom annotations usually use this method

@Target, which indicates where the annotation is used to mark. The default is to mark any element, and the value of ElementType can be assigned to it:

  1. ElementType.CONSTRUCTOR: used to describe the constructor

  2. ElementType.FIELD: member variables, objects, properties (including enum instances)

  3. ElementType.LOCAL_VARIABLE: used to describe local variables

  4. ElementType.METHOD: used to describe methods

  5. ElementType.PACKAGE: used to describe the package

  6. ElementType.PARAMETER: used to describe the parameters

  7. ##ElementType.TYPE: used to describe the class, interface (Including annotation type) or enum declaration

  8. ##@Inherited defines the relationship between the annotation and the subclass. The @Inherited meta-annotation is a mark annotation. @Inherited elaborates on a certain annotation. Types are inherited. If an annotation type modified with @Inherited is used for a class, this annotation will be used for subclasses of that class.

We can customize an annotation:

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 水果名称注解
 */
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface FruitName {
    String value() default "";
}
Copy after login

The above meta-annotation is for custom annotation services.

3. Summary

In short, an annotation is a mark used to mark code. We can implement different annotation methods by scanning different annotations. Through Java's dynamic proxy and reflection, we can Easily obtain the content of our annotation tags to operate the classes or methods we have written. In the next article, I will define a custom annotation and write its implementation.

The above is the detailed content of An article to help you understand annotations in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve: Java annotation error: Annotation parameter type is wrong How to solve: Java annotation error: Annotation parameter type is wrong Aug 18, 2023 am 11:12 AM

How to solve: Java annotation error: Wrong annotation parameter type Introduction: In Java development, annotation (Annotation) is a form of metadata used to add additional information to program elements (classes, methods, fields, etc.). However, sometimes we may encounter issues with annotation parameters being of the wrong type, which can lead to compilation errors or runtime exceptions. This article will introduce how to solve Java annotation parameter type errors and provide code examples to help readers better understand. Understanding annotation parameter type error: Annotation parameter type error

How to use annotation functions to implement custom annotations in Java How to use annotation functions to implement custom annotations in Java Oct 24, 2023 am 10:32 AM

How to use annotation functions in Java to implement custom annotations. Annotation is a special syntax element in Java that can be used to add metadata information to the code for parsing and processing at runtime. Java provides some predefined annotations (such as @Override, @Deprecated, etc.), and also supports user-defined annotations. In some scenarios, using custom annotations can make the code more concise and readable. This article will introduce how to use

How to understand the scope and life cycle of Java annotations? How to understand the scope and life cycle of Java annotations? May 03, 2024 pm 06:06 PM

The scope of an annotation determines which parts of the code they apply to, while the lifetime describes how long they live in the code. The scope has element level, declaration type level and code block level, and the life cycle is divided into compile time, class loading time and run time. The life cycle of annotations includes being added to the class file during compilation, processed by the JVM when the class is loaded, and accessible through reflection at runtime.

Annotation processor in Java Annotation processor in Java Jun 09, 2023 am 09:14 AM

Annotation Processor in Java An annotation processor in Java is a tool that can detect and process annotations in Java code. Using annotation processors can enhance compile-time checking, generate additional code, and even modify existing code, thereby improving code readability, maintainability, and reusability. Annotation processors are usually written in Java rather than interpreted and executed at runtime. This provides a lot of convenience for the annotation processor, such as the use of a richer Java type system, object-oriented features and

Methods to solve Java annotation parsing exception (AnnotationParsingException) Methods to solve Java annotation parsing exception (AnnotationParsingException) Aug 20, 2023 am 10:41 AM

Methods to solve Java annotation parsing exception (AnnotationParsingException) Introduction: In Java development, annotations have become a very important technology, which can describe various information in the program by adding metadata to the source code. In the process of using annotations, sometimes we may encounter an AnnotationParsingException exception. This exception represents an error that occurs when parsing annotations. This article will explain how to solve this

In-depth understanding of Java annotation development experience and suggestions In-depth understanding of Java annotation development experience and suggestions Nov 22, 2023 pm 05:40 PM

In-depth understanding of Java annotation development experience and suggestions. With the development of the Java language, annotations have become an indispensable part of Java development. As a kind of metadata, annotations can add additional descriptive information to the code and help developers better understand the code logic. At the same time, annotations can also be processed during compilation and runtime to achieve automated functions. In daily Java development, we often use annotations. However, to deeply understand and effectively apply annotations, you need to master a

How to fix: Java annotation error: undefined annotation How to fix: Java annotation error: undefined annotation Aug 17, 2023 am 11:30 AM

How to solve: Java annotation error: undefined annotation. In the process of using Java development, annotation is a very common technical means that can be used to add some additional information or behavior to the code. However, sometimes we may encounter an error: undefined annotation. This problem will cause the program to not work properly when compiling or running, so it is very important to solve this error. This article will introduce some methods to solve the undefined annotation error and provide some code examples. 1. Check the annotation guide package. When we use a self-

How to use annotation functions in Java for custom annotations and metadata processing How to use annotation functions in Java for custom annotations and metadata processing Oct 20, 2023 am 11:49 AM

How to use annotation functions in Java for custom annotations and metadata processing Introduction: In Java programming, annotation is a special syntax structure that can attach additional metadata to the code and be interpreted by the compiler and processor or other tools for processing. An annotation function is a special annotation that can be used to mark functions, methods, or method parameters, and these annotations can be accessed and processed through the reflection mechanism at runtime. This article will introduce how to use annotation functions for custom annotations in Java

See all articles