What will be printed as the result of the following code execution?

public class GoodbyeWorld {    
    public static void main(String[] args) {
        System.out.print(A.i);
        System.out.print(B.i);
    }
}

class A {
    static {
        i = 2;
    }
    static int i = 1;
};

class B {
    static int i = 1;
    static {
        i = 2;
    }
};
Explanation
Initialization of all static fields and static initialization blocks are executed one after another in the order in which they were written.
Therefore the field i will have a value of 1 in a class A and a value of 2 in class B.

Следи за CodeGalaxy

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

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