在LinkedList中 为成员变量都添加了transient修饰,但是真的就不被序列化了吗?看下图
在序列化之后,经过反序列化list中的值还是存在的。
原因:
这是因为LinkedList内部重写了writeObject()方法,所以序列化的时候走的自己的方法而不是ObjectOutputStream的writeObject()方法。
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
}