default values in an annotation. : Annotation « Reflection « Java

default values in an annotation.

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

@interface MyAnno {
  String str() default "Testing";

  int val() default 9000;

class Meta3 {
  public static void myMeth() {
    Meta3 ob = new Meta3();

    try {
      Class c = ob.getClass();

      Method m = c.getMethod("myMeth");

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

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

  public static void main(String args[]) {


Related examples in the same category

1.Uses reflection to display the annotation associated with a method.
2.Get annotation by annotation class
3.Show all annotations for a class and a method.
4.Does a method have an annotation
5.Get Annotation Parameter
6.A better concise toString method for annotation types
7.Find Annotated Method
8.Find Annotated Fields
9.Get default annotation value
10.Get annotation value
11.Is Field Annotation Present
12.Get Annotated Declared Fields