search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
32 views
asked Sr Member 3.9k points 5 8 12

1 Answer

0 votes
 
 
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// An annotation type declaration. 
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
  String str();
 
  int val();
}
 
public class Main {
  @MyAnno(str = "Annotation Example", val = 100)
  public static void myMeth() {
    Main ob = new Main();
    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[]) {
    myMeth();
  }
}
 
 
 
answered Advisor 8.4k points 10 15 18

Related questions

0 votes
1 answer 23 views
23 views
asked omari Sr Member 3.9k points 5 8 12
0 votes
1 answer 21 views
21 views
asked rock Expert 15k points 15 23 30
0 votes
0 answers 51 views
0 votes
1 answer 16 views
16 views
asked omari Sr Member 3.9k points 5 8 12
0 votes
1 answer 18 views
18 views
asked sunny Expert 11.4k points 14 22 28
...