天天看點

代碼執行順序

初始化執行代碼塊的順序:

(1)在初次new 一個Child類對象的時候,發現其有Parent類,則先加載Parent類,再加載Child類。

(2)加載Parent類

初始化Parent類的static屬性,賦預設值;

執行Parent類的static初始化代碼塊;

(3)加載Child類

初始化Child類的static屬性,賦預設值;

執行Child類的static初始化代碼塊;

(4)建立Parent類對象

初始化Parent類的非static屬性,賦預設值;

執行Parent的instance初始化塊;

執行Parent類的構造方法;

(5)建立Child類的對象

初始化Child類的非static屬性,賦預設值;

執行Child類的instance初始化塊;

執行Child類的構造方法。

加載驅動程式的幾種方法

  1. Class.forName(“com.microsoft.sqlserver.jdbc.ServerDriver”);
  2. DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  3. System.setProperty(“jdbc.drivers”,“com.mysql.jdbc.Driver”);