Java Record Static Field Compilation Fix

Answer Correct answer: E — Make the regNo variable static.

Given the code fragment: Which action enables the code to compile? - image

  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

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.

Community Insight

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.

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.

Community Discussion (5 comments)

ASPushkin 👍 1 Selected: 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
Uteman 👍 2
E is correct Records only allow static variables
xplorerpj 👍 1 Selected: E
E is correct answer
minhdev 👍 2
E is correct answer. record allows static variable.
james2033 👍 4 Selected: 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"); } }

Comments & Corrections

No comments 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.

Submitted for moderation before publishing. Keep it helpful and respectful.

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

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 →

Related Analysis

← Back to 1Z0-829 Study Guide