上級 › static フィールドの初期化順序 › パターン4
static フィールドの初期化順序
宣言順に初期化されます。
1 public class Main { 2 static int x; 3 static { x = 1; } 4 static { x_____; } ^ 5 public static void main(String[] args) { 6 System.out.println(x); 7 } 8 }
出力: 1期待: 2 (コンパイルは通っています)
空欄を埋めてビルドを通す
複数のstaticブロックは宣言順に実行されます。後のブロックが前の結果を上書きしていないか確認します。
次の問題