Java 17 Silver ラボ

学習の画面へ

record にメソッドを追加

出力はどれか。

public class Main {
    public static void main(String[] args) {
        Item a = new Item("pen", 100);
        Item b = a.withPrice(120);
        System.out.println(a.price() + " " + b.price() + " " + Item.count());
    }
}

record Item(String name, int price) {
    static int created = 0;

    Item {
        created++;
    }

    Item withPrice(int p) {
        return new Item(name, p);
    }

    static int count() {
        return created;
    }
}
  1. A 100 120 2
  2. B 120 120 2
  3. C 100 120 1
  4. D コンパイルエラーになる
正解と解説を見る

正解: A

ポイント

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