Last Updated: February 25, 2016
·
1.149K
· felipebrnd

Java Annotations - Retention Policy

Almost always i decide to use a custom Annotation in my code for some fancy feature, i forgot to set the RetentionPolicy, this should be a post note so i probably won’t forget it anymore.

@RetentionPolicy is an annotation for annotations (inception here), which tells when an Annotation should be disposed.

The options are:

  • RetentionPolicy.SOURCE, your annotations will be dropped off during compilation and won't be available during your code execution.
  • RetentionPolicy.CLASS, will maintain the annotations after compiling but they won't be available during your code execution (i confess i don't see the point of this option).
  • RetentionPolicy.RUNTIME, your annotation will be maintained and available during execution.