Practice mode Java

Given record P(int x, int y) {} and a variable p of type P, which expression reads the component x from another class?

Correct answer C. p.x()

Explanation

For each component, a record automatically has a public accessor method with the same name as the component, so p.x() is correct. Records do not follow the JavaBean getX naming pattern. The field x is private and final, so p.x fails outside the record's own body, and there is no general get method.

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.