The below code was taken from A Programmer�s Guide to
Java Certification, 2nd edition:
public class MyClass {
public static void main(
String[] args) {
MyClass obj = new MyClass(l);
}
static int i = 5;
static int l;
int j = 7;
int k;
public MyClass(int m) {
System.out.println(i + �, � + j + �, � + k + �, � + l + �, � + m);
}
{ j = 70; l = 20} // Instance initializer block
static {i = 50; } // Static initializer block
}
the output of the above code is: 50, 70, 0, 20, 0
I assumed that the constructor is run (which would mean that the initializer block is executed) before the main method is executed which would make the value of m = 20 but this was not the case. My question is if the constructor is only called if an instance of the class is created with �new�. ie if the object �obj� is not created would the constructor still be called ?
I also thought the initializer block is executed before execution of the main method is started, but this does not appear to be the case� ?