18

I simply don't understand the following code's execution flow:

class Test {
    static String s1 = getVal();
    static String s2 = "S2";

    private static String getVal() {
        return s2;
    }

    public static void main(String args[]) {
        System.out.println(s2); // prints S2
        System.out.println(s1); // prints null
    }
}

It is supposed to print S2 at second println statement. I am more interested in understanding why this happens rather than a solution.

0

4 Answers 4

20

Static things gets executed in the order they appear in the code.

static String s1 = getVal();

So starting with first line, s1 gets evaluated and by that time s2 is still null. Hence you see the null value.

0
16

static variables and static initializer blocks are initialized in the order they appear in the source code (except of static final variables that get initialized before non-final static variables).

s1 is initialized before s2, therefore the call to getVal() returns the default value of s2, which is null.

You can change the order of the static variables in order to initialize s2 first:

static String s2 = "S2";
static String s1 = getVal();

Another way to force the initialization of s2 to take place before that of s1 is to make s2 final:

static String s1 = getVal();
static final String s2 = "S2";

Following is an excerpt of the initialization order as stated in JLS 12.4.2. Detailed Initialization Procedure. The sections related to static variables are highlighted.

For each class or interface C, there is a unique initialization lock LC. The mapping from C to LC is left to the discretion of the Java Virtual Machine implementation. The procedure for initializing C is then as follows:

  1. Synchronize on the initialization lock, LC, for C. This involves waiting until the current thread can acquire LC.

  2. If the Class object for C indicates that initialization is in progress for C by some other thread, then release LC and block the current thread until informed that the in-progress initialization has completed, at which time repeat this step.

  3. If the Class object for C indicates that initialization is in progress for C by the current thread, then this must be a recursive request for initialization. Release LC and complete normally.

  4. If the Class object for C indicates that C has already been initialized, then no further action is required. Release LC and complete normally.

  5. If the Class object for C is in an erroneous state, then initialization is not possible. Release LC and throw a NoClassDefFoundError.

  6. Otherwise, record the fact that initialization of the Class object for C is in progress by the current thread, and release LC.

    Then, initialize the static fields of C which are constant variables(§4.12.4, §8.3.2, §9.3.1).

  7. Next, if C is a class rather than an interface, then let SC be its superclass and let SI1, ..., SIn be all superinterfaces of C that declare at least one default method. The order of superinterfaces is given by a recursive enumeration over the superinterface hierarchy of each interface directly implemented by C (in the left-to-right order of C's implements clause). For each interface I directly implemented by C, the enumeration recurs on I's superinterfaces (in the left-to-right order of I's extends clause) before returning I.

    For each S in the list [ SC, SI1, ..., SIn ], if S has not yet been initialized, then recursively perform this entire procedure for S. If necessary, verify and prepare S first.

    If the initialization of S completes abruptly because of a thrown exception, then acquire LC, label the Class object for C as erroneous, notify all waiting threads, release LC, and complete abruptly, throwing the same exception that resulted from initializing S.

  8. Next, determine whether assertions are enabled (§14.10) for C by querying its defining class loader.

  9. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

2

As per the JLS section 12.4.2, the static fields are initialised like this:

Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

So s1 is initialised first, at which point s2 is not initialised, so it has the default value of String, which is null.

And then s2 gets initialised to "S2" but s1 stays null.

Just change the order of the two declarations to solve this problem.

2

s1 is initialized first and the value of s2 is null at that time. s1 is trying to return the value of s2 which has been initialized later. That's why you are getting it as null.

If you try like this you will get the expected answer 'S2'

class Test {

    static String s2 = "S2";
    static String s1 = getVal();
    private static String getVal() {
        return s2;
    }

    public static void main(String args[]) {
        System.out.println(s2); // prints S2
        System.out.println(s1); // prints S2
    }
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.