0

Why we give class literals of annotation to the getAnnotation method? For example I want to get the class annotation and put Annotation preceding the Class.

@interface MyAnno {
 String str();
 int val();
}
class JavaApplication22 {

Why I can't get the annotation of the Class with second method?How class literals work in this situation?

MyAnno anno = m.getAnnotation(MyAnno.class); //First Method
MyAnno anno = m.getAnnotation(MyAnno);       //Second Method
3code
  • 75
  • 6
  • `m.getAnnotation(MyAnno)` (where `MyAnno` is a type) is not valid Java code. What do you think it should do? – Radiodef Mar 29 '18 at 22:10
  • I don't except m.getAnnotation(MyAnno) works.But I don't understand how MyAnno.class expression works.What it gives us?I print MyAnno.class and gives output like this "interface javaapplication22.MyAnno". – 3code Mar 29 '18 at 22:29
  • See https://stackoverflow.com/q/2160788/2891664 and https://docs.oracle.com/javase/tutorial/extra/generics/literals.html – Radiodef Mar 29 '18 at 22:30
  • Thank you.Is Class<> ancestor of other all class? In this example MyAnno.class returns Class.But print says that it is an interface. – 3code Mar 29 '18 at 22:36

0 Answers0