What the following code will type?

public class Test {
    {
        System.out.println("Block");
    }
    int x = getX();

    static {
        System.out.println("Static block");
    }

    public int getX() {
        System.out.println("X variable");
        return 5;
    }

    static int y = getY();
    public static int getY() {
        System.out.println("Y variable");
        return 6;
    }


    public static void main(String[] args) {
        Test m = new Test();
    }
} 
Explanation
Firstly the static initialization blocks are performed and the static fields are initialized (in the order in which they are written), so "Static block" and "Y variable" trigger.
Then the simple initialization blocks are performed and the non-static fields are initialised (in the order in which they are written), so "Block" and "X variable" trigger.

Следи за CodeGalaxy

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

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