Creates and uses a single-member annotation : Annotations Types « Language « Java Tutorial






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

@Retention(RetentionPolicy.RUNTIME)
@interface MySingle {
  int value(); // this variable name must be value
}

class Single {
  @MySingle(100)
  public static void myMeth() {
    Single ob = new Single();

    try {
      Method m = ob.getClass().getMethod("myMeth");

      MySingle anno = m.getAnnotation(MySingle.class);

      System.out.println(anno.value()); // displays 100

    } catch (NoSuchMethodException exc) {
      System.out.println("Method Not Found.");
    }
  }

  public static void main(String args[]) {
    myMeth();
  }
}








1.13.Annotations Types
1.13.1.Annotations and Annotation Types
1.13.2.The Annotation Interface
1.13.3.Using Default Values
1.13.4.Marker Annotations
1.13.5.Single-Member Annotations
1.13.6.Creates and uses a single-member annotation
1.13.7.The single-value syntax
1.13.8.Some Restrictions
1.13.9.A marker annotation