Mega Code Archive

 
Categories / Java Tutorial / Language
 

Marker Annotations

Marker Annotations are used to mark a declaration. A marker annotation is a special kind of annotation. A marker annotation contains no members. Using isAnnotationPresent( ) to determine if the marker is present. import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { } @MyAnnotation public class MainClass {   // Annotate a method.   @MyAnnotation()   public static void myMethod() {   }   public static void main(String[] arg) {     try {       MainClass ob = new MainClass();       Method m = ob.getClass( ).getMethod("myMethod");       Annotation[] annos = m.getAnnotations();       System.out.println("All annotations for myMeth:");       for(Annotation a : annos)       System.out.println(a);     } catch (Exception exc) {     }   } } All annotations for myMeth: @MyAnnotation()