I'm looking at the topic of annotations in the Oracle documentation . And looking at this example:
@ClassPreamble (
author = "John Doe",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Jane Doe",
// Note array notation
reviewers = {"Alice", "Bob", "Cindy"}
)
public class Generation3List extends Generation2List {
// class code goes here
}
I still don't understand what it can do for me. For example, if I remove the annotation it still works; So what are they generally used for? They explained to me that they are used for interfaces but until then I understood.
Interesting question in which I want to contribute my grain of sand.
What are @notations?
Annotations are a form of metadata that provides data about a program that is not part of the program itself. Annotations have no direct effect on the behavior of the code they annotate.
Annotations have a number of uses, including:
Annotation Basics
The format of an annotation
In its simplest form, an annotation looks like the following:
The sign character (
@
) indicates to the compiler that what follows is an annotation. In the following example, the annotation name isOverride
:The annotation can include elements , which can be named or unnamed, and there are values for those elements:
If there is only one element named value, the name can be omitted, as in:
If the annotation has no elements, the parentheses can be omitted, as shown in the
@Override
.It is also possible to use multiple annotations on the same declaration:
If the annotations have the same type, then it is called a repeating annotation:
Repeated annotations are supported as of Java SE 8. For more information, see Repeated Annotations .
The annotation type can be one of the types that are defined in the Java SE API
java.lang
or packages.java.lang.annotation
In the examples above,Override
andSuppressWarnings
are predefined Java annotations. It is also possible to define your own annotation type.Author
The and annotationsEbook
in the example above are custom annotation types.Where can I use annotations?
Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used in a declaration, each annotation usually appears, by convention, on its own line.
As of the Java SE 8 release, annotations can also be applied to the use of types. Here are some examples:
Class instance creation expression:
new @Interned MyObject();
Issued type:
MyString = (cadena @NonNull) str;
clause
implements
:class UnmodifiableList<T> implements @Readonly List<@Readonly T> { ... }
Exception statement thrown:
void monitorTemperature() throws @Critical TemperatureException { ... }
This form of annotation is called a type annotation . For more information, see Type Annotations and Pluggable Type Systems .
Declaring an annotation type
Many annotations replace comments in code.
Suppose a software group traditionally begins the body of each class with comments that provide important information:
To add this same metadata with an annotation, you must first define the annotation type. The syntax to do this is:
The annotation type definition is similar to an interface definition in that the keyword interface is preceded by the at sign (@) (@ = AT, as in the annotation type). Annotation types are a form of interface, which will be covered in a later lesson. At the moment, it is not necessary to understand the interfaces.
The body of the annotation definition above contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.
Once the annotation type is defined, you can use annotations of that type, with the values filled in, as follows:
Note: For the information in to
@ClassPreamble
appear in the Javadoc-generated documentation, you must annotate the definition of@ClassPreamble
with the annotation@Documented
:And there is more
If you want to do a doctorate in @notations, I leave these links that complete the Java doc tutorial:
I also had my doubts about annotations until I read this very easy and well explained MEDIUM publication https://medium.com/@alvareztech/java-annotations-b8ae69739b4e . I hope you find it useful.
I give you an example of a widely used annotations, the @override tells the compiler to override the method of the parent class, of course this is when there is class inheritance.