Writing custom Annotation with RentionPolicy

In this article we are going to learn about how to use of retention policy in Java Annotations.
The retention policy determines at what point annotation should be discarded.
Java supports 3 types of retention policies through java.lang.annotation.RetentionPolicy enumeration. They are
  • SOURCE
    The retention policy SOURCE will be retained only with source code, and discarded during compile time.

  • CLASS
    The retention policy CLASS will be retained till compiling the code, and discarded during runtime.

  • RUNTIME
    The retention policy RUNTIME will be available to the JVM through runtime.

The retention policy will be specified by using java built-in annotation @Retention, and we have to pass the retention policy type.
The default retention policy type is CLASS.
The following example describes, how to define annotation using Retention policy RUNTIME.
SOURCE:AuthorInfo.java

SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment