pretty rusty but im pretty sure i've never seen a code written like this. it is a mock question from java associate exam could someone tell me whether the 'static' in line 10 is connected to the go() method?? and mainly why is the output is x y c g ???
public class testclass {
testclass() {
System.out.print("c ");
}
{
System.out.print("y ");
}
public static void main(String[] args) {
new testclass().go();
}
void go() {
System.out.print("g ");
}
static {
System.out.print("x ");
}
}
static { System.out.print("x "); }
This is the static initializer block. This will be called at the time of class loading. Thus first call.
{ System.out.print("y "); }
This is non static initializer block. Will be called the first thing once a object is created.
testclass() { System.out.print("c "); }
This is constructor. Will be executed in the process of object creation, after all initializer blocks are executed.
Lastly,
void go() { System.out.print("g "); }
Normal method call. Last thing to be executed.
For more details, please refer http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html