Home  >  Article  >  Java  >  How to use @Retention in java

How to use @Retention in java

王林
王林forward
2023-05-25 13:55:07837browse

Explanation

1. @Retention defines the length of time that Annotation is retained: some Annotations only appear in the source code, and others that are abandoned by the compiler are compiled in the class file. Some Annotations may be ignored by the virtual machine, and others are compiled in the class file (please be careful not to affect the execution of the class, because Annotation and class are separated during use). Use this meta-Annotation to limit the life cycle of Annotation.

Function

2. Indicates the level at which the annotation information needs to be saved. It is used to illustrate the life cycle of the annotation, that is, in which range the annotation is valid.

Example

@Target({ElementType.TYPE})  //用在描述类、接口或enum
@Retention(RetentionPolicy.RUNTIME)  //运行时有效
public @interface MyClassAnnotation {
    String value();  //这个MyClassAnnotation注解有个value属性,将来可以设置/获取值
}

What are the characteristics of Java

1.As a representative of static object-oriented programming language, Java language realizes object-oriented theory, Allows programmers to perform complex programming with elegant thinking.

2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, and dynamic nature.

3. Use Java to write desktop applications, Web applications, distributed systems and embedded system applications, etc.

The above is the detailed content of How to use @Retention in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete