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.*;

@interface Anno{
    int value() default 5;
public class Test {
    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);

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
Зарегистрируйся сейчас
или Подпишись на будущие тесты