How can serialisation/deserialisation break immutability?

后端 未结 7 642
小鲜肉
小鲜肉 2021-02-07 15:54

I was asked this question in an interview. The interviewer wanted to know how to make an object immutable. and then he asked what if I serialise this object - will it break immu

7条回答
  •  迷失自我
    2021-02-07 16:02

    An immutable object is one which cannot be changed once created. You can create such an object by using private access modifiers, and the final keyword.

    If an immutable object was serialized, its raw bytes could be modified so that upon deserialization the object is no longer the same.

    This can't be prevented completely. Encryption, checksums, and CRC's will help to prevent this though.

提交回复
热议问题