I'm having trouble understanding the following code's execution. I want to follow the Java program so that I can understand how everything works together. I step up breakpoints in Eclipse but it doesn't explain why. Here's the code:
public class Sequence {
public Sequence() {
System.out.print("c ");
}
{
System.out.print("y ");
}
public static void main(String[] args) {
new Sequence().go();
}
void go() {
System.out.print("g ");
}
static {
System.out.print("x ");
}
}
The output to the code is x y c g
. Can someone explain why this is? I thought the program entry point was public static void main
but it appears static
executes first?
static
block? It is not a method (in the source), it is an initialization block, ie. happens first.c
first but it prints outx
. I don't understand why.