serialVersionUID best practices

B

Bent C Dalager

I am about to write a Serializable class that I expect will change in
the future. I want old serialized streams to be readable by later
versions of the class and so I will eventually have to tackle the
serialVersionUID issue.

As I see it, there are two main approaches to this. First, there is
the naïve one:

1) Write the class and use it for serialization.
2) Change the class and find, to your surprise, that you can no longer
read old streams.
3) Obtain the serialVersionUID of the original class and end up adding
a line something like
static final long serialVersionUID = 7110175216435025451L;
to your new, better version of the class
4) Take care to ensure that future changes are compatible according to
the serialization protocols.

Then there is the preventive approach:

1) Decide that the class you write will probably change in the future,
so add the line
static final long serialVersionUID = 1;
to your class sooner rather than later.
2) Write the class and use it for serialization.
3) Take care to ensure that future changes are compatible according to
the serialization protocols.

My question then becomes, is the last one better than the first one in
any way? The only real difference is that the serial number in the
latter is cuter than in the former. Is there a third approach that is
better than both of them?

Cheers
Bent D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top