Java Serialization readObject Access Modifier

1Z0-829 By islever Editorial Last updated

Short answer

B Cookie 0.0 2.99

Question

Given the Product class: and the Shop class: What is the result? - Exhibit: Java Serialization readObject Access Modifier - Exhibit: Java Serialization readObject Access Modifier
  1. Compilation fails.
  2. Cookie 0.0 2.99 Correct Answer
  3. Cookie 2.99 2.99
  4. Cookie 3.99 2.99
  5. An exception is produced at runtime.

Key Takeaways

Tests knowledge that the deserialization hook must be private; if public, it is ignored and transient variables retain their initial defaults.

Determines the output of a Java program involving serialization when the private readObject method is incorrectly declared as public, causing transient fields to remain default values.

Common mistake: Learners often select C or D, assuming the custom logic runs regardless of visibility, missing the strict contract requirements of the Serialization API.

Expert Analysis

Why the Answer Is Correct

The correct output is B because the readObject method in the Product class is declared as public. According to Java's serialization specification, for the custom readObject method to be invoked during deserialization, it must have the signature private void readObject(ObjectInputStream ois). Since it is public, the reflection mechanism does not recognize it as a valid callback. Consequently, the default deserialization process occurs, leaving the transient field price at its default primitive value of 0.0, while the non-transient name is restored.

Why the Other Options Are Wrong

Options C and D imply that the readObject method executed successfully, which is incorrect due to the access modifier violation. Option A is incorrect because the code compiles fine; the error is logical/runtime behavior, not syntax. Option E is incorrect because no exception is thrown; the serialization simply proceeds without invoking the custom logic.

Community Comment Notes

Community consensus correctly identifies B. As user SrinivasJasti noted, "Since it is public,loses its special status in the serialization process." User Langton also confirmed that "for readObject method to be called it has to be difined as private which is not the case here."

Exam Strategy

Memorize the exact signature for serialization hooks: they must be private. Any deviation in visibility (protected, package-private, public) causes the JVM to ignore them.

Frequently Asked Questions

Can readObject be protected?

No, it must be strictly private to be recognized by the serialization runtime.

What happens to transient fields if readObject is ignored?

They are not restored from the stream and revert to their default initialization values (e.g., 0.0 for double).

More 1Z0-829 FAQ →

Community Votes

B
100%

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

Discussion 4 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 4

  1. supersquax 👍 3
    B correct, verified in online compiler!
  2. Langton 👍 2
    Answer is B because for readObject() method to be called it has to be difined as private which is not the case here
  3. SrinivasJasti 👍 1 Answered B
    The Java Serialization API is designed to recognize only a private readObject method for deserialization purposes. Since it is public ,loses its special status in the serialization process.The transient fields are not restored to their intended values threfore price remains 0.0 leading to Cookie 0.0 2.99
  4. xplorerpj 👍 1 Answered B
    B is correct

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