How do you think, what is the output for following program?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AreStaticFieldsInitializedBeforeConstructorIsCalled { | |
public static void main(String[] args) { | |
new A(); | |
} | |
} | |
class A { | |
static A a = new A(); | |
static String staticField = "1"; | |
static final String CONSTANT = "2"; | |
public A() { | |
System.out.println(staticField); | |
System.out.println(CONSTANT); | |
} | |
} |
Output is:
null
2
1
2
Constructor is called before "staticField" is initialized.