Java 17 Silver ラボ

学習の画面へ

this() で別のコンストラクタを呼ぶ

出力はどれか。

public class Main {
    public static void main(String[] args) {
        new P();
    }
}

class P {
    P() {
        this("x");
        System.out.print("A");
    }

    P(String s) {
        System.out.print("B" + s);
    }
}
  1. A BxA
  2. B ABx
  3. C A
  4. D コンパイルエラーになる
正解と解説を見る

正解: A

ポイント

「メソッドとコンストラクタの作成」をこのサイトで解く (記録・間違えた問題の解き直し・AI教師への質問)