上級 › static フィールドの初期化順序 › パターン1
static フィールドの初期化順序
宣言順に初期化されます。
1 public class Main { 2 static int a = Main.b + 1; 3 static int b = 2; 4 public static void main(String[] args) { 5 System.out.println(______); ^ 6 } 7 }
出力: 1期待: 3 (コンパイルは通っています)
空欄を埋めてビルドを通す
staticフィールドは書いた順に初期化されます。まだ0のbを使ってaが計算されます。
次の問題