12 March 2007

Serial Version UID

In this post i will mention about very popular Eclipse warning.

First i wanna say if you are seeing "Declare Serial Version Uid"
And if you do not wanna see this warning, if you are getting anger when you
see this.

Ok sir.

Do this :)

On Eclipse Window>Preferences>Java>Compiler>Errors\Warnings>Potential programming problems
Select Ignore item
"Serializzable Class Without Serial VersionUID" From Drop Down List Click OK.

Did you do the above?

if yes :)

I have one good,
one bad,
news.

Good new is that you will never see such warning any more :)
Bad new is that you really need
to see such warning :D
So RollBack in order not to live sleepless nights :)

Therefore Please go:
Window>Preferences>Java>Compiler
>Errors\Warnings>Potential programming problems

and Select
Warning item "Serializable Class Without Serial VersionUID"
From Drop Down List Click OK.

For Pretty Sure You Will ask WHY?

at this 2004-07-13 day at 12:18
a bug report to Eclipse organization.
Bug number is:69942 and detailed explanation is:

"There should be a compiler warning for serializable classes that do not
explicitly declare serialVersionUID. For maximum effectiveness, this compiler
option should be "warning" by default.

Serializable classes that do not declare serialVersionUID are a serious
problem for people who build products that rely on Java serialization. The
problem is that many people forget that changing their class will introduce
compatibility problems with earlier releases of their product, and that the
compiler's computation of a SUID is so fragile that it is not recommended.
In one case, only 50 of a product's 1500 serializable classes declared the
SUID, making it difficult for them to provide inter-version compatibility.
Best practice dictates that one always declares serialVersionUID. (There are
lots more things that need to be done to manage Java serializability, but this
is a cornerstore of policy.)"


After this post it is fixed in eclipse 3.1
now when you see this warning you can hold ctrl button and press 1(press ctrl-1)
If you do this eclipse will generate a unique serial verison uid for your class.

like this private static final long serialVersionUID = -403250971215465050L;

But you may ask yourself Ecilpse is not the one and only IDE that java development
has been done How can JVM hadle this Serializable classes?

This is also good question.

Java take your field names in the class. Mix them and generate such a static number,
if you do not declare but problem arises here also since you may need to change field names.

Josuha Bloch also mentions about this in his famous book(Efective java.)

"One issue is the serial version UID.
Every serializable class has a unique identification number associated with it.
If you do not specify the identification number explicitly by declaring a
private static final long field named serialVersionUID,
the system automatically generates it by applying a complex deterministic procedure to the class…
If you change [the class] in any way … the automatically generated serial version UID changes.
If you fail to declare an explicit serial version UID, compatibility will be broken.
Bloch, Joshua. Effective Java. p. 214"


To generate serial version UID you may use serialver tool which comes with the Eclise SDK
like this:

>>serialver -classpath 'build;C:/fenerbahce/gfb.jar' edu.yeditepe.cse.CLASSNAME

if your class is not a Serializable (if your class doesn't implement java.io.Serializable interface
or does not extend any Serializable class ) then you do not need to declare such a static variable.

By the way you will not take such a warning in this case also.


as i search thru. internet you may have very severe problems if you do not declare serialVersionUID
in your Serializable class. Since somewhere it will be declared and it will be out of your control.

4 comments:

Anonymous said...

The post is excellent. It helped me solve a long time doubt.
Thank you. Cheers!

Anonymous said...

Big to you thanks for the necessary information.

Anonymous said...

I apologise, but, in my opinion, you are not right. I can prove it. Write to me in PM, we will discuss.

Unknown said...

what is wrong with this post?,

Any critique precious,

let us know.