MyAnno.java Source code

Java tutorial

Introduction

Here is the source code for MyAnno.java

Source

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

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
    String str() default "Testing";

    int val() default 1;
}

public class Main {
    @MyAnno()
    public static void myMeth() throws Exception {
        Main ob = new Main();
        Class c = ob.getClass();
        Method m = c.getMethod("myMeth");
        MyAnno anno = m.getAnnotation(MyAnno.class);
        System.out.println(anno.str() + " " + anno.val());
    }

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