Advance Java

Learn New things from Rixosys

What is Annotations Java?


Annotations in computer programming languages provide data about a program that is not part of the program itself by decorating them. They does not impact directly to the operation of the code to which they annotate.

Annotations are mainly used for:

  • Information for the compiler: Compiler uses the annotations to detect errors or suppress warnings.
  • Compiler-time and deployment time processing: annotation information is processed by the annotation processor in order to produce new source code and other files.
  • Runtime processing: some annotations are scanned at runtime by using various techniques and open source libraries.

We can apply annotations to a program's declarations of classes, fields, methods, and other program elements. Rather than directly affecting on the program's semantic, they do affect the way programs are treated by tools and libraries, that can affect the semantics of an executing program. We can read the annotations from the class file, source files, or at run time.

Annotations are processed by compiler plug-ins called annotation processors whenever java source code is compiled. Processors are capable of producing  informational messages, create additional Java source files or resources which in turn may be compiled and processed. But processors cannot modify the annotated code itself. The Java compiler stores annotation metadata in the class files only when the annotation has a RetentionPolicy of CLASS or RUNTIME, that is used to determine how to interact with the program elements or to change their behavior.