很生锈,但我很确定我从来没有见过这样写的代码。这是一个模拟问题从Java伙伴考试可能有人告诉我是否'静态'在第10行连接到go()方法?主要是为什么输出是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 ");
}
}
可能重复:http://stackoverflow.com/questions/2943556/static-block-in-java – user432
'静态{}'被添加到的静态初始化类。当这个类被初始化时,它从上到下执行。 –
ahh ..这就解释了为什么要首先打印x.谢谢ya'll – Leonne