上級 › super() と this() の呼び出しルール › パターン4
super() と this() の呼び出しルール
必ず先頭に置く必要があります。
1 class Base { 2 Base(int n) {} 3 } 4 public class Main extends Base { 5 int x = 5; 6 Main() { super(______); } ^ 7 public static void main(String[] args) { new Main(); } 8 }
Main.java:6: error: cannot reference x before supertype constructor has been called
空欄を埋めてビルドを通す
super()の引数の中では、まだ自分自身のフィールドは初期化されていません。親のコンストラクタが終わるまでインスタンスは存在しない扱いです。
次の問題