Java Record Static Field Compilation Fix

1Z0-829 By islever Editorial Last updated

Short answer

E Make the regNo variable static.

Question

Given the code fragment: Which action enables the code to compile? - Exhibit: Java Record Static Field Compilation Fix
  1. Replace record with void.
  2. Replace record with class.
  3. Remove the regNo initialization statement.
  4. Make the regNo variable public.
  5. Make the regNo variable static. Correct Answer

Key Takeaways

The exam traps candidates who do not know that record components are implicitly final instance fields, meaning any additional field must be static.

This question tests the rule that Java records cannot have non-static instance fields. Replacing the initialization with a static variable is the correct way to compile this code.

Common mistake: Candidates often choose option C, thinking the error is due to duplicate initialization, failing to realize the fundamental issue is the instance nature of the field.

Expert Analysis

Why the Answer Is Correct

The Java Language Specification (JLS) explicitly states that a record declaration may contain only static members in its body. The code fragment attempts to declare regNo as an instance variable (int regNo = 100;). Since records automatically generate final instance fields for their components, adding another non-static instance field results in a compile-time error. Making regNo static (Option E) satisfies the JLS requirement.

Why the Other Options Are Wrong

Option A is invalid because void is not a type that can be used as a class or record modifier in this context. Option B (class) would work if the field remained non-static, but the question asks what action enables the record code to compile, and changing it to a class changes the type entirely. Option C fails because even if you remove the explicit initialization, the compiler still rejects non-static fields in records. Option D is incorrect because access modifiers do not change the instance/static nature of the field.

Community Comment Notes

The community consensus is unanimous, with all votes pointing to E. As ASPushkin noted, referencing "jls17 8.10", the spec forbids non-static fields. Users like Uteman confirmed that "Records only allow static variables". This confirms the strict nature of record definitions in modern Java.

Official Reference

https://docs.oracle.com/en/java/javase/17/language/records.html

This link opens the official vendor documentation in a new tab.

Exam Strategy

Always check if a data structure is a Record. If it is, remember that only static fields are allowed in the body. Do not confuse record components (which are instance fields) with additional fields defined in the body.

Frequently Asked Questions

Why can't I add a non-static field to a Java record?

Records are designed to be immutable data carriers. Non-static fields would break the implicit immutability and canonical constructor contract.

Can I use a class instead of a record here?

Yes, a standard class allows non-static instance fields, but the question specifically asks how to fix the record code.

More 1Z0-829 FAQ →

Community Votes

E
100%

100% of anonymous learners picked answer E. Votes are pick records left by other test-takers — they are not the verified answer.

Discussion 5 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 5

  1. james2033 👍 4 Answered E
    package q06; record Product(int pNumber, String pName) { // int regNo = 100; static int regNo = 100; public int getRegNumber() { return regNo; } } public class App { public static void main(String[] args) { Product p1 = new Product(1111, "Ink Bottle"); } }
  2. minhdev 👍 2
    E is correct answer. record allows static variable.
  3. Uteman 👍 2
    E is correct Records only allow static variables
  4. ASPushkin 👍 1 Answered E
    answer: E "It is a compile-time error for the body of a record declaration to contain a non-static field declaration" jls17 8.10
  5. xplorerpj 👍 1 Answered E
    E is correct answer

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

← Back to 1Z0-829 Study Guide