6

What is the intended need for RetentionPolicy.CLASS and RetentionPolicy.SOURCE. In which annotation scenario, We can use these? I wanted some examples.

From the Java doc:

CLASS: Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.

  • In which scenario, We will go for this as developer?

SOURCE: Annotations are to be discarded by the compiler.

  • In which scenario, We will go for this as developer?
  • Possible duplicate of [Annotation SOURCE Retention Policy](http://stackoverflow.com/questions/8048941/annotation-source-retention-policy) – Lachezar Balev Mar 10 '16 at 07:26
  • Possible duplicate of [How do different retention policies affect my annotations?](http://stackoverflow.com/questions/3107970/how-do-different-retention-policies-affect-my-annotations) – António Ribeiro Mar 10 '16 at 07:56

1 Answers1

-1

RetentionPolicy use to control the Lifetime and visibility of the annotations

RetentionPolicy.CLASS - The defined annotation will be stored in the .class file, but not available at runtime. This is the default retention policy if you do not specify any retention policy at all.

RetentionPolicy.SOURCE - The defined annotation will be ignored by the compiler when building the code. So the annotation is only available in the source code, and not in the .class files, and not at runtime too.

Use two annotations depending on the use case with that .class files are not polluted unnecessarily.

If the annotation is used by build tools that scan the code, you can use RetentionPolicy.SOURCE retention policy.

Yuresh Karunanayake
  • 519
  • 1
  • 4
  • 10