according to Java Annotation API:
RetentionPolicy.CLASS Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
RetentionPolicy.RUNTIME Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.
I am looking for a sample of "CLASS" retention policy. when we need to use this policy instead of RUNTIME policy.
Retention Policy: A retention policy determines at what point an annotation is discarded. It is s specified using Java's built-in annotations: @Retention [About] 1. SOURCE: annotation retained only in the source file and is discarded during compilation.
Annotation Type RetentionIndicates how long annotations with the annotated type are to be retained.
Annotations are used to provide supplemental information about a program. Annotations start with '@'. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.
Notice the @interface keyword. This signals to the Java compiler that this is a Java annotation definition. Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name.
CLASS Annotations are used in obfuscator tools like http://proguard.sourceforge.net . For example annotation @KeepName disables name mangling when you need to have your class name unchanged to be able to call methods like Class.forName().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With