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
Зареєструйся Зараз
або Підпишись на майбутні тести