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
You can also read
- Introduction to Java Annotations
- How to write custom annotation
- Writing custom Annotation with RentionPolicy
- How read annotation at runtime using Java Reflection
- How to list out annotations used in a class using Java Reflection
- How to create custom annotation with default values
- How to create Marker Annotation using java
0 comments :
Post a Comment