1Z0-829 · Question #28
record Product(int pNumber, String pName) { int regNo = 100; public int getRegNumber() { return regNo; } } public class App { public static void main(String[] args) { Product p1 = new Product(1111…
The correct answer is E. Make the regNo variable public. Note: The provided answer key appears to be incorrect. Option C (make regNo static) is the actual fix, not E. --- In Java, record types are compact data carriers - the Java Language Specification explicitly prohibits instance fields in a record body beyond the declared record…
Question
Options
- AReplace record with void.
- BRemove the regNO initialization statement.
- CMake the regNo variable static.
- DReplace thye regNo variable static
- EMake the regNo variable public
How the community answered
(51 responses)- A2% (1)
- B2% (1)
- C4% (2)
- E92% (47)
Explanation
Note: The provided answer key appears to be incorrect. Option C (make regNo static) is the actual fix, not E.
In Java, record types are compact data carriers - the Java Language Specification explicitly prohibits instance fields in a record body beyond the declared record components (pNumber, pName). The line int regNo = 100; is an illegal instance field, causing a compile error.
Why C is correct: Declaring regNo as static int regNo = 100; is permitted - records allow static fields freely. The field is no longer per-instance state, so the restriction doesn't apply.
Why the distractors fail:
- A -
voidis a return type, not a class/type keyword; it cannot replacerecord. - B - Removing the declaration leaves
regNoused but undefined ingetRegNumber(), still a compile error. - D - Appears to be a typo/duplicate option; not a meaningful choice.
- E - Changing visibility to
publicdoes nothing to address the rule; it's still an instance field, still illegal.
Memory tip: Think of a Java record as a sealed envelope - its contents (instance state) are fixed at construction via the header parameters. Any extra data you want to attach must be static (shared at the class level), not tucked inside the individual envelope.
Topics
Community Discussion
No community discussion yet for this question.