Java – findbugs wants readObject (…) private serialization. Why?

I run findbugs on some code and it says that the readObject (...) method must be private to be called for serialization / deserialization? Why? What would be the problem if it were made public?

Solution

Readobject() / writeobject() is private. Here is the transaction: if your class bar extends some classes foo; Foo also implements readobject() / writeobject(), and bar also implements readobject() / writeobject()

Now, when the bar object is serialized or deserialized, the JVM needs to automatically call readobject() / writeobject() (that is, there is no need to explicitly call these superclass methods) for Foo and bar However, if these methods are not private, they will become method overrides, and the JVM can no longer call superclass methods on subclass objects

So they must be private!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>