What will be displayed after the following code execution?

public class Tasks {
    public static Tasks instance = new Tasks();
    private static final int DELTA = 5;
    private static int BASE = 7;
    private int x;

    public Tasks() {
        x = BASE + DELTA;
    }

    public static void main(String[] args) {
        System.out.println(Tasks.instance.x);
    }
}
Explanation
Initialization of static fields is performed in order in which they were declared.
In this case, a instance field will be initialized first and a BASE will be initialized later. Therefore, at the time when Tasks() constructor is called a BASE field contains a value of 0.
A DELTA field is a constant. Therefore compiler replaces it with its value.
Thus, x = 0 + 5; is a result.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback