Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6597263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:11:13+00:00 2026-05-25T18:11:13+00:00

I am reading the serialization chapter in Effective Java . I am trying to

  • 0

I am reading the serialization chapter in Effective Java. I am trying to understand the paragraph below, which is found in the book.

If you implement a class with instance fields that is serializable and extendable,there is a caution you should be aware of. If the class has invariants that would be violated if its instance fields were initialized to their default values (zero for integral types, false for boolean, and null for object reference types), you must add this readObjectNoData method to the class:

// readObjectNoData for stateful extendable serializable classes
private void readObjectNoData() throws InvalidObjectException {
    throw new InvalidObjectException("Stream data required");
}

I am not sure what that statement means.

To test this, I created a class called Person (both serializable and extendable)

class Person implements Serializable {

    private String name;
    private int age;

    Person() {
        this("default", 1);
    }
    
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

– and a class: Employee, which extends it.

class Employee extends Person {

    String address;

    public Employee() {
        super();
        address = "default_address";
    }

    public Employee(String name, int age, String address) {
        super(name, age);
        this.address = address;
    }
}

Are there any invariants in the Person class I created? When will they be violated? I copy-pasted the code for the readObjectData() method in the Employee class, but it never got called. When will the readObject() method be called? Am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:11:13+00:00Added an answer on May 25, 2026 at 6:11 pm

    The readObjectNoData section in Java Object Serialization Specification
    seems interesting (see below).

    Your edits to the question give a perfect example. If Employee was serialized when it did not extend Person and later deserialized when it did then the Person part would be initialized to empty string and 0 age. Using this method, you can initialize them to “name” and 1 respectively.

    For serializable objects, the readObjectNoData method allows a class
    to control the initialization of its own fields in the event that a
    subclass instance is deserialized and the serialization stream does
    not list the class in question as a superclass of the deserialized
    object. This may occur in cases where the receiving party uses a
    different version of the deserialized instance’s class than the
    sending party, and the receiver’s version extends classes that are not
    extended by the sender’s version. This may also occur if the
    serialization stream has been tampered; hence, readObjectNoData is
    useful for initializing deserialized objects properly despite a
    “hostile” or incomplete source stream.

    private void readObjectNoData() throws ObjectStreamException;

    Each serializable class may define its own readObjectNoData method. If
    a serializable class does not define a readObjectNoData method, then
    in the circumstances listed above the fields of the class will be
    initialized to their default values (as listed in section 4.5.5 of The
    JavaTM Language Specification, Second Edition); this behavior is
    consistent with that of ObjectInputStream prior to version 1.4 of the
    JavaTM 2 SDK, Standard Edition, when support for readObjectNoData
    methods was introduced. If a serializable class does define a
    readObjectNoData method and the aforementioned conditions arise, then
    readObjectNoData will be invoked at the point during deserialization
    when a class-defined readObject method would otherwise be called had
    the class in question been listed by the stream as a superclass of the
    instance being deserialized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been doing some reading up on XML serialization, and from what I understand,
I posting a doubt that I came across reading Effective Java. I apologize if
I use XML serialization for the reading of my Config-POCOs. To get intellisense support
I'm reading about program specialization - specifically java and I don't think I quite
Reading this question I found this as (note the quotation marks) code to solve
I was reading custom serialization article on msdn: http://msdn.microsoft.com/en-us/library/ty01x675%28VS.80%29.aspx It mentions that there are
I am reading the book Spring Recipes. I am not able to comprehend certain
Normally I write a class and add XML Serialization to it for my web
I am preparing for MCTS exam 70-536 and reading the book MCTS self paced
I am reading Thinking in Java 4th Edition . There described a strange workaround

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.