I am reading the chapter on Serialization in Effective Java.
Who calls the readObject() and writeObject()? Why are these methods declared private ?
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?