Annotations are just metadata, nothing more. When you want to provide some information about a class, you put annotations on it. Think of them (to some extent) as an alternative to the old well-known XML way of defining metadata.
Now, it is obvious that someone is reading your XML and running code that does something with metadata. The same thing happens with annotations: the structure to which the annotation belongs is responsible for reading the annotation and creating something with this information. In the case of @NotNull , its hibernate-validator project. The java open API allows access to information in annotations by reflection (classes such as java.lang.Class , Method , Field , etc.). So somewhere inside the hibernate validator there is code that goes into your class, reads the annotations by reflection, and checks if the class adheres to these annotations. These annotations usually have a “temporary” retention policy, which means they are stored in bytecode and loaded with the class that contains these annotations.
There are also annotations that should be handled by the Java compiler. For example @Deprecated , @SuppressWarnings , etc. The benefit of this annotation is that you may find some problems with the code at compile time.
You can also place annotation handlers and “hook” them at the compilation stage, but has a completely different story.
Hope this clarifies the use of annotations a bit
source share