What will be the result of compilation and execution of the following code?

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


@Retention(RetentionPolicy.RUNTIME)
@interface Anno{
    int value() default 5;
}
public class Test {
    @Anno(10)
    public static void test(int i) { }
    public static void main(String [] args) {
        Method m = new Test().getClass().getMethod("test");
        Anno anno = m.getAnnotation(Anno.class);
        System.out.println(anno.value());
    }
}
Explanation

Compilation error.

Method Class.getMethod() can throw NoSuchMethodException. It is a checked exception.

Therefore, it must be caught using try ... catch ... inside the main method or declared in the throws clause of method declaration.


Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты