关于类方法块的调用顺序,一直没有进行总结记录过,中午趁着午饭时间,稍微温习下最基础的内容,并记录下。
静态代码块、普通代码块、构造函数块:
1 public class Test { 2 3 public static void main(String[] args) { 4 5 new Square(); 6 System.out.println("--------Next---------"); 7 new Circle(); 8 9 } 10 } 11 class Shape { 12 13 public Shape() { 14 System.out.println("Shape Constructor."); 15 } 16 17 static { 18 System.out.println("Shape static code."); 19 } 20 21 { 22 System.out.println("Shape normal code."); 23 } 24 25 } 26 class Square extends Shape{ 27 28 public Square() { 29 System.out.println("Square Constructor."); 30 } 31 32 static { 33 System.out.println("Square static code."); 34 } 35 36 { 37 System.out.println("Square normal code."); 38 } 39 40 } 41 class Circle extends Shape { 42 43 public Circle() { 44 System.out.println("Circle Constructor."); 45 } 46 47 static { 48 System.out.println("Circle static code."); 49 } 50 51 { 52 System.out.println("Circle normal code."); 53 } 54 55 }