フリー問題

Java SE 21 Developer Professional のフリー問題 10 / 20 問目

問題文

A record is declared with two components and needs to reject negative values for the second component. Which approach follows the intended design of record classes?

選択肢

  1. A compact constructor that validates the parameter and then assigns both fields, which its body has to spell out.
  2. Declare a static factory method that validates the value and make the canonical constructor private so callers cannot bypass the validation, while the compact form is left unused.
  3. Override the automatically generated accessor for the second component so that it throws when the stored value turns out to be negative at the time it is read.
  4. Declare a compact constructor that validates the parameter and leaves the assignments to the two fields for the compiler to add.

解答・解説を確認するには

正解と解説の確認、回答の記録には無料登録が必要です。登録すると演習モードでフリー問題に回答し、正誤と解説をその場で確認できます。