Annotation Details

Contact Us or call 1-877-932-8228
Annotation Details

Annotation Details

  • Annotations are defined as a sort of interface, but an @ symbol precedes the interface keyword in the declaration (as in @interface). When used, an @ symbol is prepended to the name.
  • They can be parameterized with optional elements.
    • A parameter element named value is special - if it is the only element, then it does not need to be named when used (a single value passed to the annotation will be assumed to be the value).
    • For annotations accepting only one parameter, that parameter should be named value.
    • An annotation with no parameters serves as a marker, much like implementing the Serializable interface.
  • They are used as modifiers preceding any target code entities that are declared: class, field, method, constructor, method parameters, return values, package, or local variables.
  • Based on their specified retention policy, they can be discarded after compilation (the SOURCE policy), or preserved into the compiled class (CLASS persists, but the JVM isn't required to keep the information after the class is loaded, and RUNTIME annotations do remain with the class in the JVM).
    • @Override is an example of the source type, since it is only needed by the compiler
    • A runtime type that you might encounter the effect of is @Deprecated, which states that an element is deprecated - you will receive a compiler warning if your code uses an element marked with this annotation (and, since you might be accessing the element in an already-compiled class, this annotation must persist into the compiled class file).
    • @SuppressWarnings is another source annotation, with an optional element to specify what types of warnings are to be suppressed.
  • The annotation definitions are themselves annotated: -@Target and @Retention are used before the annotation definition to specify which type of element receives the annotation, and what the retention is.
Next