nerdexam
Oracle

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…

Java Object-Oriented Approach

Question

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, "Ink Bottle"); } } Which action enables the code to compile?

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)
  • A
    2% (1)
  • B
    2% (1)
  • C
    4% (2)
  • E
    92% (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 - void is a return type, not a class/type keyword; it cannot replace record.
  • B - Removing the declaration leaves regNo used but undefined in getRegNumber(), still a compile error.
  • D - Appears to be a typo/duplicate option; not a meaningful choice.
  • E - Changing visibility to public does 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

#Records#Instance fields#Initialization#Syntax rules

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice