Single-Member Annotations : Annotations Types « Language « Java Tutorial






  1. Single-Member Annotations contains only one member.
  2. Single-Member Annotations allow a shorthand form of specifying the value.
  3. The name of the member must be value.
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 {
  int value();
}

@MyAnnotation(102)
public class MainClass {
  // Annotate a method.
  @MyAnnotation(101)
  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(value=101)








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