Java Serializable: Importance of serialVersionUID?

Possible Duplicate:
Why should I bother about serialVersionUID?

I am using a class that implemented Serializable. Eclipse gives me a "No Serial UID" warning, but the writing and reading the class works fine. I was wondering what is the purpose of a Serializable class having a UID? Is it just good practice or will my class not work without it eventually? Can it be any number?


It is important when you serialize and de-serialize the same class using different VMs or on different machines. The UID is used to check if the class you are trying to de-serialize is the really the one you think it is, so if your class in your code has one UID, and the class that was serialized has a different one, the de-serialization will fail.

A class without a UID will be given one automatically by the JVM, but there is no guarantee that different JVMs will give the same UID to the same class.

Read the javadoc of the Serializable interface for more details.

Can it be any number?

Ideally it should be some number unique to that particular version of your class (like a hash), if you make changes to your class that affect serialized fields (non- transient ), the UID needs to change. I think Eclipse can generate a UID automatically for you.

链接地址: http://www.djcxy.com/p/23838.html

上一篇: 是否需要手动定义SerialVersionUID?

下一篇: Java可序列化:serialVersionUID的重要性?