Home > Java > javaTutorial > How to use Java annotation Annotaton

How to use Java annotation Annotaton

WBOY
Release: 2023-04-25 17:58:15
forward
1302 people have browsed it

1. Three basic Annotaton

@Override: To limit a method, it is to override the parent class method. This annotation can only be used for methods
@Deprecated: Used to represent a certain program Elements (classes, methods, etc.) are obsolete
@SuppressWarnings: Suppress compiler warnings

@Override

class father{
   public void fly(){}
}
class son extends father{
    @Override
    public void fly() {
        super.fly();
    }
}
Copy after login

Interpretation

@Override means that son overrides the fly method

Details

If there is no @Override, the fly method will still be rewritten

class father{
   public void fly(){}
}
class son extends father{
    public void fly() {
        super.fly();
    }
}
Copy after login

If the @Override annotation is written, the compiler will check whether the method overrides the parent class method , if rewritten, the compilation will pass. If it is not rewritten, a compilation error occurs.

How to use Java annotation Annotaton

@Override can only modify methods, not other classes, packages, properties, etc.

//@Override底层代码
@Target(ElementType.METHOD)//ElementType.METHOD说明@Override只能修饰方法
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Copy after login

@Deprecated

public class Deprecatedtext {
    public static void main(String[] args) {
        father father1 = new father();
        father1.fly();
    }
}
@Deprecated
class father{
    @Deprecated
   public void fly(){}
}
Copy after login

Interpretation

@Deprecated means that a certain program element (class, method, etc.) is obsolete, and will be reminded by a horizontal line in the middle of the word. Indicates that use is not recommended.

Effect

How to use Java annotation Annotaton

Details can modify methods, classes, packages, parameters, etc.

//@Deprecated底层代码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})//说明Deprecated能修饰方法,类,包,参数等等
public @interface Deprecated {
}
Copy after login

2.@Deprecated can play the role Make the compatibility transition between old and new versions

@SuppressWarnings

@SuppressWarnings("all")
class father{
   public void fly(){}
}
Copy after login

Interpretation

@SuppressWarnings annotation can be used to suppress warning information{""}Write the warning information you want to suppress

Effect

How to use Java annotation Annotaton

How to use Java annotation Annotaton

Details

1. The scope of @SuppressWarnings is related to the location where you place it

public class Enumtext {
    @SuppressWarnings("all")//作用范围在main方法
    public static void main(String[] args) {
        father father1 = new father();
        father1.fly();
    }
}
@SuppressWarnings("all")//作用范围在father类
class father{
   public void fly(){}
}
Copy after login

The specified warning type is

all, suppress all warnings
boxing, suppress warnings related to packaging/disassembly operations
cast, suppress warnings related to forced transformation operations
dep-ann, suppress warnings related to elimination comments
deprecation, suppress warnings related to elimination
fallthrough, suppress warnings related to omission of break in switch statements
finally, suppress and not returned Warnings related to finally blocks
hiding, suppressing warnings related to local variables that hide variables
incomplete-switch, suppressing warnings related to missing items in switch statements (enum case)
javadoc, suppressing warnings related to javadoc related warnings
nls, suppress warnings related to non-nls string literals
null, suppress warnings related to null value analysis
rawtypes, suppress warnings related to the use of raw types
resource, Suppress warnings related to the use of resources of type Closeable
restriction, suppress warnings related to the use of deprecated or forbidden references
serial, suppress warnings related to the omission of the serialVersionUID field in serializable classes
static- access, suppress warnings related to incorrect static access
static-method, suppress warnings related to methods that may be declared static
super, suppress warnings related to replacement methods that do not contain super calls
synthetic-access, suppresses warnings related to unoptimized access to internal classes
sync-override, suppresses warnings related to missed synchronization due to overriding synchronization methods
unchecked, suppresses warnings related to unchecked operations
unqualified-field-access, suppress warnings related to unqualified field access
unused, suppress warnings related to unused code and disabled code

How to use Java annotation Annotaton

Meta-annotation

  • Retention specifies the scope of the annotation, three types of SOURCE, CLASS, RUNTIME

  • Target specifies the annotation. Where to use

  • Documented to specify whether the annotation will be reflected in javadoc

  • Inherited The subclass will inherit the parent class annotation

Retention

  • RetentionPolicy.SOURCE: After the compiler uses it, discard the comment directly

  • RetentionPolicy.CLASS: Compiler Record the annotations in the class file, and the JVM will not retain the annotations when running java

  • RetentionPolicy.PUNTIME: The compiler records the annotations in the class file, and the JVM will retain the annotations when running java

Retention case

@Override the bottom layer (the shortcut key for IDEA to enter the bottom layer is Ctrl B)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)//表示@Override在编译器使用后,直接丢弃注释
public @interface Override {
}
Copy after login

Target

The value of Target
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
//   1.CONSTRUCTOR:用于描述构造器
    2.FIELD:用于描述域
    3.LOCAL_VARIABLE:用于描述局部变量
    4.METHOD:用于描述方法
    5.PACKAGE:用于描述包
    6.PARAMETER:用于描述参数
    7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
Copy after login
Target case

@Deprecated underlying

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})//表示@Documented在这些地方可以写注解
public @interface Deprecated {
}
Copy after login

Documented

Documented case

@Deprecated bottom layer

@Documented//@Deprecated代码会被保存到生产的文档中
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
Copy after login

Inherited

The Annotation modified by it will have inheritance. If a class uses an Annotation modified by @Inherited, its subclasses will automatically have this annotation

The above is the detailed content of How to use Java annotation Annotaton. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template