publicclassSubStaticBlockextendsStaticBlock{ { System.out.println("Just one sub normal line!"); }
privatefinalstaticint e = 8; { System.out.println("I am sub final static field e!"); }
privatestaticint d = 7; { System.out.println("I am sub static field d!"); }
static { System.out.println("I am sub static block!"); }
privatestaticint a = 7; { System.out.println("I am sub static field a!"); }
privatefinalstaticint b = 8; { System.out.println("I am sub final static field b!"); }
publicSubStaticBlock(){ System.out.println("I am sub constructor!"); }
publicint c = 9; }
子类与父类在打印语句里面最大的区别就是多了一个sub对吧。
好,我们先单纯地看一下父类的代码执行情况:
1 2 3 4 5 6 7 8
I am static block! Just one parent normal line! I am final static field e! I am static field d! I am static field a! I am final static field b! I am constructor! c is 9
I am static block! I am sub static block! Just one parent normal line! I am final static field e! I am static field d! I am static field a! I am final static field b! I am constructor! Just one sub normal line! I am sub final static field e! I am sub static field d! I am sub static field a! I am sub final static field b! I am sub constructor! c is 9