靜態代碼塊、非靜態代碼塊和構造方法

執行順序:

靜態代碼塊 > 非靜態代碼塊 > 構造方法

示例:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章