If you aren’t familiar with Serialization in Java, then today is your lucky day! Here’s a quick, crash course in Java Serialization:

Serialization allows instances of classes (i.e. objects) created at runtime to be saved and then later restored at a different runtime. While most developers could probably do this on their own with a GetState() and LoadState() method, Java can automagically do this on virtually any object by preserving its state to a sequence of bytes. All a Java developer has to do is add “implements Serializable” to their class definition, and voila! they can now save state with an ObjectOutputStream and instantiate states into objects with an ObjectInputStream.

While it can be just that simple, Java’s Serialization API provides all sorts of other features. One of which is the serialVersionUID. According to the documentation,

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long.

A pretty handy feature I might say. Without it, the API would need to rely on the compiled assembly’s version number (which might change on each compile) even if the class definition remains the same. But with a serialVersionUID, developers can be in control of whether or not two versions of a class definition are the same.

So now that you’re familiar with Java Serialization[*], what might you do when presented with a serialVersionUID? Probably not what the original developer on Stephen McLaughry’s project did, as discovered by Howard Lewis Ship

private static final long serialVersionUID = RandomUtils.nextLong();

 

[*] Sure, go ahead and add “familiar with Java Serialization” as a bullet point on your résumé. You’re welcome.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!