Trying to understand serialVersionUID
so I'm coding up my own event using Eclipse (for pretty much the first time), and it gives me a warning of
The serializable class CaptureEvent does not declare a static final serialVersionUID field of type long
now ... first I notice that it's a warning, not an error. second, after looking up stuff on the api, it appears that the serialVersionUID can be any number I want - as long as it's unique? I asked a friend what the point of it is, and he said it's for objects that implement the Serializable interface to guarantee that it can only be accessed by one object at a time. (useful for threads, mostly, to prevent deadlock situations and such) That makes perfect sense to me, but I'm not sure how to go about implementing it. Do I simply have to make up any long I want? If that's the case, how do I know the one I pick will be unique? Does it need to be declared as public?
public static final long serialVersionUID = <anyLongIWant>L;
Please help me to understand this better. ^.^
[1030 byte] By [
JJCoolBa] at [2007-11-27 4:56:00]

> now ... first I notice that it's a warning, not an
> error. second, after looking up stuff on the api, it
> appears that the serialVersionUID can be any number I
> want - as long as it's unique?
it can be any number you want. it is used as a version number for (de)serialization. if you deserialize an object and the version of the serialized form is different from the one in the current class, you will get an error. you should start with any value, and change it only if you change your class to be incompatible with previously serialized versions, in other words when you want to get an error
>I asked a friend what
> the point of it is, and he said it's for objects that
> implement the Serializable interface to guarantee
> that it can only be accessed by one object at a time.
> (useful for threads, mostly, to prevent deadlock
> situations and such)
that is absolute ******** - this has nothing to do with threading. your friend is full of it
> That makes perfect sense to
> me,
if you have indeed read the api docs, you should know that it makes no sense at all
> but I'm not sure how to go about implementing
> it. Do I simply have to make up any long I want?
> If that's the case, how do I know the one I pick
> will be unique? Does it need to be declared as
> public?
i think you should read the API docs again.
> it can be any number you want. it is used as a
> version number for (de)serialization. if you
> deserialize an object and the version of the
> serialized form is different from the one in the
> current class, you will get an error. you should
> start with any value, and change it only if you
> change your class to be incompatible with previously
> serialized versions, in other words when you want to
> get an error
So, in other words, if I change the code and released a newer version I'd want a different UID so that an older version couldn't use the newer code?
> if you have indeed read the api docs, you should know that it makes no sense at all
I meant that what my friend said made sense - even if he might have been mistaken.
> i think you should read the API docs again.
I haven't *read* the API docs - I just looked a couple of things up on the API about EventObject and the Serializable interface. Do you have a url you could direct me to that describes it?
> So, in other words, if I change the code and released
> a newer version I'd want a different UID so that an
> older version couldn't use the newer code?
No. Only change the number if you are sure that (a) there are no persistent instances of the serialized object in existence or (b) you are prepared to throw them all away. Otherwise don't change it.
> Do you have a url you could direct me to that describes it?
Everybody does. It's in the Java doc/Guide to Features/Serialization.
ejpa at 2007-7-12 10:11:05 >

> Everybody does. It's in the Java doc/Guide to> Features/Serialization.I must be missing something obvious 'cause I can't find it on the sun.com site. :(
You can't find the Javadoc?
ejpa at 2007-7-12 10:11:05 >

I go to the Java API Documentation pretty routinely. But I can't find the "Guide to Features" anywhere. I'm starting to think that when people say "Javadoc" they don't mean the API Documentation. heh
> I go to the Java API Documentation pretty routinely.
> But I can't find the "Guide to Features" anywhere.
> I'm starting to think that when people say
> "Javadoc" they don't mean the API Documentation. heh
Click on a class within the java.io package, and then click on the package link at the top. You will then see a package description for java.io. Scroll down on that page. It has two links, one is to the Java Object Serialization Specification.
Kaj
kajbja at 2007-7-12 10:11:05 >

HAH! Now that's funny. Thanks, Kaj! I've never looked at Package Specs before, so that helps me a lot. :) Unfortunately, when I try to access " Java Object Serialization Specification" link, it gives me a "page not found" 404. *is chuckling* Well -- I'll try it again tomorrow.
Thanks again, everyone. ^.^
> HAH! Now that's funny. Thanks, Kaj! I've never
> looked at Package Specs before, so that helps me a
> lot. :) Unfortunately, when I try to access " Java
> Object Serialization Specification" link, it gives me
> a "page not found" 404. *is chuckling* Well -- I'll
> try it again tomorrow.
>
> Thanks again, everyone. ^.^
I guess you are reading the Java 5 doc? I had the same problem with the link there. Try to do the same with the javadoc for Java 6. That link works.
Kaj
kajbja at 2007-7-12 10:11:05 >

The 'Guide to Features' link is on the second line of http://java.sun.com/j2se/1.5.0/docs/index.html. Hard to miss really.Follow the links for Object Serialization->Serialization Specification. Works for me.
ejpa at 2007-7-12 10:11:05 >

> The 'Guide to Features' link is on the second line of
> http://java.sun.com/j2se/1.5.0/docs/index.html. Hard
> to miss really.
I've always gotten to the API via http://java.sun.com/reference/api/index.html which doesn't have that info anywhere on it.
And ok - I'll check those out. Thanks again. Off to work for now.