Deep understanding of Java annotations and examples
Java annotation
What is annotation?
Annotations in Java are the metadata of Java source code, that is, annotations are used to describe java source code. The basic syntax is: @ followed by the name of the annotation.
① Override: a method that identifies whether a method has correctly overridden its parent class. ② Deprecated: indicates that this class member is no longer recommended. It is a tag annotation. ③ Suppresswarnings: used to suppress warnings, etc.
To better understand the annotation, we can write an annotation ourselves
@Target: used to limit where annotations can be used. For example, you can use @ target to limit the number of classes and methods you can use
@Retention: This is used to control the state of annotation compilation in class. By default, it is compiled into class but not read out. There are two other states that are not compiled into class. The last one is over compiled and can be read out through class reflection
@Documented: it is possible to generate annotations using the network
@Inherited: can subclasses inherit annotations
So the following is our own annotation class
Test, learn to use
Read through class reflection
If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!