Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called—even the constructor.
static Test1 k = new Test1();//这条语句将不会再执行
因为它是静态的,已经驻留在内存中
值时,
编译器会自动将这些叙述依序搬到class constructor内。同样地,当class具有instancefield,且直接在宣告处透过「=...」的方式设定其值时,编译器会自动将这些叙述
依序搬
到instance constructor内。而对于class constrcutor,把其他static字段搬入内的次序依声明次序依次加入。对于instance constructor,也会加入各个sinstance field,但其次序是先所有的instance field,最后为instance constructor。
当Test1()构造完毕时候开始显示了。 所以先打印TestCons, 然后依次打印出几个static 到这里知识对aa() 初始化完毕。。 并且所有的static 不再被打印。
于是开始执行Test1 的main
打印MainBegins 再次构造Test1.
aa2 aa4被打印。
-->
static Test1 k = new Test1();
-->
aa b = new aa(2); (System.out.println ("aa"+m);)
-->
aa d = new aa(4); (System.out.println ("aa"+m);)
-->
System.out.println ("Test1Cons");
-->
aa a = new aa(1); (System.out.println ("aa"+m));
-->
System.out.println ("MainBegin");
new Test1();
-->aa b = new aa(2);
-->aa d = new aa(4);
-->System.out.println ("Test1Cons"); -->
static aa c = new aa(3); (System.out.println ("aa"+m));