0
点赞
收藏
分享

微信扫一扫

LinkedList中的成员变量为什么要用transient修饰?

boomwu 2021-09-25 阅读 54
日记本


在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);
    }
举报

相关推荐

0 条评论