可做如下簡單測試:
calss A { static int a; public A{ System.out.println("constructor of A"); } static { System.out.println("static block"); } public static void main(String[] args){ A aclass = new A(); A bclass = new A(); } }測試結果:
測試結論:static代碼塊是在構造函數之前執行的,而且只執行一次,即類首次加載時。