執行順序:
靜態代碼塊 > 非靜態代碼塊 > 構造方法
示例:
public class Test {
{
System.out.println("not static run");
}
static {
System.out.println("static run");
}
public Test() {
System.out.println("constructor run");
}
}
public class TestString {
public static void main(String[] args) {
Test test1 = new Test();
Test test2 = new Test();
Test test3 = new Test();
}
}
輸出結果:
static run
not static run
constructor run
not static run
constructor run
not static run
constructor run