I am studying for the Java SE 7 exam and I am looking at sample questions. I cannot seem to figure out why the following program returns in the order x y c g
.
I understand why the x
is run first, because it is a static initialisation block but can someone please explain why y
is run before c
and g
?
public class Triangle {
Triangle() {
System.out.print("c ");
}
{
System.out.print("y ");
}
public static void main(String[] args) {
new Triangle().go();
}
void go() {
System.out.print("g ");
}
static {
System.out.print("x ");
}
}