Friday, 27 June 2014

RetentionPolicy.RUNTIME

RetentionPolicy.RUNTIME: The marked annotation is retained by the JVM so it can be used by the runtime environment.

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotation1{
    String value();
}

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.CLASS)
@interface MyAnnotation2{
    String value();
}

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    String value();
}

import java.lang.annotation.*;

@MyAnnotation1("Using MyAnnotation1")
@MyAnnotation2("Using MyAnnotation2")
@MyAnnotation3("Using MyAnnotation3")
public class RetainEx {
    public static void main(String args[]){
        Class myClass = RetainEx.class;
        Annotation ann[] = myClass.getDeclaredAnnotations();
        
        for(int i=0; i< ann.length; i++){
            System.out.println(ann[i].toString());
        }
    }
}

Output
@annotations.MyAnnotation3(value=Using MyAnnotation3)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment