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

How to use Java's annotations Annotaton

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use the annotated Annotaton of Java". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the annotated Annotaton of Java.

1. Three basic Annotaton

@ Override: qualifies a method by overriding the parent method. This annotation can only be used for methods.

@ Deprecated: used to indicate that a program element (class, method, etc.) is out of date

SuppressWarnings: suppresses compiler warnings

@ Overrideclass father {public void fly () {}} class son extends father {@ Override public void fly () {super.fly ()}} interpretation

@ Override means that son overrides the fly method

Details

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

Class father {public void fly () {}} class son extends father {public void fly () {super.fly ();}}

When the @ Override annotation is written, the compiler checks to see if the method overrides the parent method, and if so, compiles. If there is no override, the compilation error.

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

/ / @ Override underlying code @ Target (ElementType.METHOD) / / ElementType.METHOD states that @ Override can only modify the method @ Retention (RetentionPolicy.SOURCE) public @ interface Override {} @ Deprecatedpublic class Deprecatedtext {public static void main (String [] args) {father father1 = new father (); father1.fly ();} @ Deprecatedclass father {@ Deprecatedpublic void fly () {}}

@ Deprecated indicates that a program element (class, method, etc.) is out of date and will be reminded by a line in the middle of the word. Indicates that it is not recommended.

Effect.

Details can be decorated with methods, classes, packages, parameters, etc. / @ Deprecated underlying code @ Documented@Retention (RetentionPolicy.RUNTIME) @ Target (value= {CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) / / Deprecated can modify methods, classes, packages, parameters, etc. Public @ interface Deprecated {}

The role of 2.@Deprecated can be used to make a compatible transition between the old and the new.

@ SuppressWarnings@SuppressWarnings ("all") class father {public void fly () {}} interpretation

The @ SuppressWarnings annotation can be used to suppress the warning message {""} to write the warning message you want to suppress.

Effect.

Details

The scope of 1.@SuppressWarnings is related to where you put it.

Public class Enumtext {@ SuppressWarnings ("all") / / scope in main method public static void main (String [] args) {father father1 = new father (); father1.fly ();}} @ SuppressWarnings ("all") / / scope in father class class father {public void fly () {}}

The specified warning types are

All, suppress all warnings

Boxing, suppressing warnings related to package / disassembly jobs

Cast, suppressing warnings related to forced transition jobs

Dep-ann, suppressing warnings related to phase-out comments

Deprecation, warnings related to suppression and elimination

Fallthrough, suppressing warnings related to omission of break in switch statements

Finally, suppressing warnings related to unreturned finally blocks

Hiding, suppressing warnings related to local variables of hidden variables

Incomplete-switch, suppressing warnings related to missing items in the switch statement (enum case)

Javadoc, suppressing warnings related to javadoc

Nls, suppressing warnings related to non-nls string text

Null, suppressing warnings related to null analysis

Rawtypes, suppressing warnings related to using raw types

Resource, suppressing warnings related to using resources of type Closeable

Restriction, suppressing warnings related to the use of unrecommended or prohibited references

Serial, suppressing warnings related to serializable categories missing serialVersionUID fields

Static-access, suppressing warnings related to incorrect static access

Static-method, suppressing warnings related to methods that may be declared as static

Super, suppresses warnings related to the replacement method but does not contain super calls

Synthetic-access, suppressing warnings related to unoptimized access to internal classes

Sync-override, suppresses the warning of missing synchronization due to replacement synchronization method

Unchecked, suppressing warnings related to unchecked jobs

Unqualified-field-access, suppressing warnings related to unqualified field access

Unused, suppressing warnings related to unused and deactivated code

Meta annotation

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

Target specifies where annotations can be used

Documented specifies whether the annotation will be reflected in javadoc

The Inherited subclass inherits the parent annotation

Retention

RetentionPolicy.SOURCE: discards comments directly after the compiler uses it

RetentionPolicy.CLASS: the compiler records comments in the class file, and JVM does not retain comments when running java

RetentionPolicy.PUNTIME: the compiler records comments in the class file, and JVM retains comments when running java

Retention case

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

@ Target (ElementType.METHOD) @ Retention (RetentionPolicy.SOURCE) / / indicates @ Override after the compiler uses it Directly discard the value @ Target of the annotation public @ interface Override {} TargetTarget (value= {CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER) TYPE}) / / 1.CONSTRUCTOR: used to describe constructor 2.FIELD: used to describe domain 3.LOCAL_VARIABLE: used to describe local variables 4.METHOD: used to describe method 5.PACKAGE: used to describe package 6.PARAMETER: used to describe parameter 7.TYPE: used to describe classes, interfaces (including annotation types), or enum declared Target cases

@ Deprecated bottom layer

@ Documented@Retention (RetentionPolicy.RUNTIME) @ Target (value= {CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) / / indicates that @ Documented can write notes public @ interface Deprecated {} DocumentedDocumented cases in these places

@ Deprecated bottom layer

The @ Documented//@Deprecated code will be saved to the production document @ Retention (RetentionPolicy.RUNTIME) @ Target (value= {CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) public @ interface Deprecated {} Inherited

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

Thank you for your reading, the above is the "Java Note Annotaton how to use" the content, after the study of this article, I believe you have a deeper understanding of the Java annotation Annotaton how to use this problem, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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