Practice mode Java

Which of these can a Java record class not declare in its body?

Correct answer A. An additional instance field

Explanation

A record's state is defined entirely by its header, so declaring any further instance field is a compile-time error. Static fields, instance methods and a compact canonical constructor, often used for validation, are all allowed. This restriction ensures that the generated equals, hashCode and toString methods describe the complete state of a record.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.