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 6587173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:53:03+00:00 2026-05-25T16:53:03+00:00

I am reading the chapter on Serialization in Effective Java . Who calls the

  • 0

I am reading the chapter on Serialization in Effective Java.

  1. Who calls the readObject() and writeObject()? Why are these methods declared private ?

  2. The below is a piece of code from the book

    // StringList with a reasonable custom serialized form
    public final class StringList implements Serializable {
        private transient int size = 0;
        private transient Entry head = null;
    
        //Other code
    
        private void writeObject(ObjectOutputStream s)
            throws IOException {
            s.defaultWriteObject();
            s.writeInt(size);
            // Write out all elements in the proper order.
            for (Entry e = head; e != null; e = e.next)
               s.writeObject(e.data);
            }
        }
    }
    

    Is there any specific reason the variable size is declared as transient and then in the writeObject method it is explicitly written? If it were not declared as transient, it would have been written anyway, right?

  • 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-25T16:53:04+00:00Added an answer on May 25, 2026 at 4:53 pm

    (1) The methods are not declared in any class or interface. A class, that implements the Serializable interface and requires special special handling during the serialization and deserialization process must implement those methods and the serializer/deserializer will try to reflect those methods.

    This is one of the rather strange corners in Java where the API is actually defined in the javaDoc… But if the methods had been defined in an interface, then they had to be public (we can’t implement an interface method an lock it by adding a private modifier).

    Why private – the javaDoc does not give a hint. Maybe they are specified as private because no other class but the implementor is intended to use them. They are private by definition.

    (2) The example simply shows how the special handling works. In this example, size is transient and will not be serialized. But now we introduce the special handler and this handler adds the value of size to the stream. The difference to the normal approach with non-transient fields could be the order of elements in the resulting stream (if it matters…).

    The example could make sense, if the transient field was defined in a super class and a subclass wanted to serialize the value.

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

Sidebar

Related Questions

I am reading the serialization chapter in Effective Java . I am trying to
I'm reading through Chapter 3 of Joshua Bloch's Effective Java . In Item 8:
I am reading the chapter on Generics in Effective Java. Help me understand difference
I've been reading Bruce Eckel's Thinking In Java and in the chapter on generics,
Reading through Thinking in Java i stuck in ex:6 of Inner Classes chapter. Exercise
I was reading i18n chapter from java blueprint and I came across this the
I've just finished reading the chapter of 'Thinking in Java' concerning type information and
I am reading the 7th chapter of Java Concurrency in Practice. In a section
I'm currently reading chapter 5.8 of Dive Into Python and Mark Pilgrim says: There
I was reading the example chapter from the book by Ayende and on the

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.