I recently had with a colleague, a very controversial discussion on "Serialization and serialVersionUID. And I started the discussion with the following designs and ideas put forward.
If a Java class can implement the interface Serializable and does not define any static class variable called serialVersionUID, you get the following warning from Eclipse:
The serializable class XYZ Does not declare a static final serialVersionUID field of type long.
I want you now to give time for reflection, whether this mechanical response is really useful. First, one must be clear about what you need serialization at all and what is the meaning of these serialVersionUID actually. This is, among other things, the case when it comes to transfer Java objects in binary form based on a network protocol (eg RMI or Spring HTTP invoker) between a client and an Application Server. If I want to send an instance of the class XYZ from client to server, the class must be serializable and XYZ be known on both the client and the server.
Here it can happen in principle, that is present on the client a different version of the class as on the server, so an inconsistency between the client and server exist, which can easily lead to a malfunction of the application. To detect such inconsistencies, but it is exactly the serialVersionUID mechanism. Constitutes the JRE is deserialized on a fixed network received instance, the serialVersionUID of this instance does not match the serialVersionUID of the loaded class, it throws an exception, so the inconsistency detected quickly and (for example, by redeployment of the application) can be adjusted.
Anyone having different opinions feel free to post here.
Bookmarks