Java Marker Annotation

A marker annotation is a special type of annotation which contains no members. Its only purpose is to mark an item. Therefore, its presence as an annotation is sufficient. The best way to determine if a marker annotation is present is to use the method named isAnnotationPresent(), which is defined by the AnnotatedElement interface.

Java Marker Annotation Example

Following is an example that uses a marker annotation. As a marker interface contains no members, simply determining whether it is present or absent is sufficient.

/* Java Program Example - Java Marker Annotation */

import java.lang.annotation.*;
import java.lang.reflect.*;

/* marker annotation */
@interface MyMarker { }

class Marker {
   /* annotate the method using marker.
   *  Notice that no { } is needed 
   public static void myMethod() {
      Marker obj = new Marker();
      try {
         Method m = obj.getClass().getMethod("myMethod");
         /* specify if the annotation is present */
            System.out.println("MyMarker is present");
      } catch(NoSuchMethodException exc) {
         System.out.println("Method not found..!!");
   public static void main(String args[])

The output of the above Java program shown below, confirms that the @MyMarker is present :

MyMarker is present

In the above program, notice that you do not need to follow the @MyMarker with parentheses when it is applied. Thus, the @MyMarker is applied by using its name, like :


It is now wrong to supply an empty set of parentheses, but they are not needed.

Java Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube