程序加载类的时候,会先加载类的静态块,当你运行main方法时,首先会加载ExplicitStatic类,加载类的静态块,static Go g1 = new Go();static Go g2 = new Go(); 同样g1被new出来后,也会首先加载Go的静态块
static String s1 = "run";
static String s2, s3;
static {
s2 = "drive car";
s3 = "fly plane";
System.out.println("s2 & s3 initialized");
}
然后才是构造方法
Go() {
System.out.println("Go()");
}
当这些都加载完成后,开始执行main方法.所以 输出的结果是
s2 & s3 initialized
Go()
Go()
Inside main()
run or drive car or fly plane
Go.s1: run
|