How can serialisation/deserialisation break immuta

2020-06-02 02:06发布

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 immutability? If yes, how do I prevent it? Can anyone please help me understand this?

标签: java
7条回答
等我变得足够好
2楼-- · 2020-06-02 02:57

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.

查看更多
登录 后发表回答