Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Organizing Data Serialization

Harry James
Harry James
14,780 Points

Benefits of using serialver over incrementing

Hey there!

I was wondering if anyone knew of what the benefits of using the serialver command is over just incrementing the value of serialVersionID.

Would it not be easier if we were to just set a Unique ID of "1" and increment it every time we make a major change?

private static final long serialVersionUID = 1L;
// Make major change:
private static final long serialVersionUID = 2L;

Thanks in advance! :)

1 Answer

Christopher Augg
Christopher Augg
21,223 Points

Harry,

It is my understanding that serialver is a tool one should only use to provide backward compatibility for a class without an explicit serialVersionUID.

For example, you are working on a Rectangle class that was written over two years ago for a project at work. You notice that while it implements Serializable, it does not have the constant serialVersionUID. You need to implement two new methods but when you try to compile/run you get a java.io.InvalidClassException.

This is because java uses fields and methods to compute the serialVerionUID for you when it is not explicitly provided. Since your new implementation had two new methods, the hash java generates is different and your modified class is incompatible with the last version of Rectangle. Therefore, you use serialver to get the hash that java assigned the previous version.

serialver Rectangle

Rectangle: private static final long serialVersionUID = -5055464683094427547L;

You then copy the constant, paste it into your Rectangle class, and serialize your modified implementation on compile. Now your modified class can maintain backward compatibility and allow you to evolve the Rectangle class by keeping that serialVersionUID.

Of course, there is no need to use serialver for any new class development. Nor would you want to do so. Just assign serialVersionUID to 0L and leave it that way for backward compatibility. This is where your question comes into play. No, there is no benefit to using serialver over assigning 1L, 2L, 3L...nL. However, incrementing is not for backward compatibility, it is for intentional backward incompatibility, and that is a good thing if it is what you need.

Regards,

Chris

Harry James
Harry James
14,780 Points

Awesome Christopher!

Thanks for the explanation. Just gave this a go and yes, it seems this works great for backwards compatibility. Didn't think of it for that use :)