@Override public String toString(){ if (strB == null) strB = "strB from delayed init"; returnsuper.toString() + "\nstatic sstrB: " + sstrB + "\nstrA: " + strB; } } ```java
在main函数中创建这两个类的实例:
```java A a = new A(); System.out.println("----------------"); System.out.println(a);
System.out.println("\n================\n");
B b = new B(); System.out.println("----------------"); System.out.println(b);
可以得到输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
A static init block start A instance init block start A constructor start ---------------- static sstrA: sstrA from init block strA: strA from init block
================
B static init block start A instance init block start A constructor start B instance init block start B constructor start ---------------- static sstrA: sstrA from init block strA: strA from init block static sstrB: sstrB from init block strA: strB from init block