我正在阅读《Effective Java》中有关序列化的章节。
谁调用了readObject()和writeObject()方法?为什么这些方法被声明为private?
以下是书中的一段代码:
// 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); } } }
变量
size
被声明为瞬态变量,然后在writeObject方法中显式写入,这是有特定原因的吗?如果它没有被声明为瞬态变量,它不是仍然会被写入吗?