We can use getAnnotations() on an interface of Annotation but not getAnnotation? Why
When I changed the interface from MyAnno to Annotation in the followng program, the compiler was not recognizing the data defined in the Annotation like the str() etc…
package british_by_blood;
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention (RetentionPolicy.RUNTIME)
@interface Hashingsnr {
String str();
int yuiop();
double fdfd();
}
public class German_by_Descent {
@Hashingsnr(str = "Annotation Example", yuiop = 100, fdfd = 4.267)
public void mymeth(){
German_by_Descent ob = new German_by_Descent();
try{
Class c = ob.getClass();
Method m = c.getMethod("mymeth");
Hashingsnr anno = m.getAnnotation(Hashingsnr.class);
System.out.println(anno.str() + " " + anno.yuiop() + " " + anno.fdfd());
}catch(NoSuchMethodException exc){
System.out.println("Method Not Found");
}
}
public static void main(String[] args) {
German_by_Descent ogb = new German_by_Descent();
ogb.mymeth();
}
}
Your program seems to be working correctly. I get the following output when i run it…
I’m i missing something in your question?
I also changed the code to use the getAnnotations() method and recieved the same result…