What will be the result of compilation and execution of the following code:

public class Test {
    static int b = Test.a;
    static int a = 3;
    static {
       System.out.println("a=" + a + ", b=" + b);
    }
}
Choose all that apply.
Explanation
The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (§8.3.2.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

( JLS 12.4.1)

@max1 thanks! fixed

2018 Feb 2, 5:35:28 PM

Can you explain why does "Runtime error" present in right answers?

2018 Jan 11, 8:23:16 AM

Follow CodeGalaxy

Mobile Beta

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