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.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes