• Post Reply Bookmark Topic Watch Topic
  • New Topic

serialVersionUID  RSS feed

 
Sreesanth Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know that serialVersionUID is implemented for version control in serialization/deserialization process. Can some one help me with exact process of creating this version UID and how that is compared during deserialization.


Thanks,
Sree
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define it however you want to, or you can let your IDE generate it for you. If the IDE does it, it's probably IDE specific, and probably is just some hash of the types and order of the non-static member variables.

As for how it is compared, it's just a long, and the behavior is that it either matches and we are successful or it doesn't and we fail:

 
Sreesanth Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:You can define it however you want to, or you can let your IDE generate it for you. If the IDE does it, it's probably IDE specific, and probably is just some hash of the types and order of the non-static member variables.

As for how it is compared, it's just a long, and the behavior is that it either matches and we are successful or it doesn't and we fail:




Thanks Jeff. So, is it like when JVM serializes an object, the generated serialVersionUID is stored inside and when same file is deserialized, the inside value is compared with the then calculated value ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreesanth Nair wrote:
Thanks Jeff. So, is it like when JVM serializes an object, the generated serialVersionUID is stored inside and when same file is deserialized, the inside value is compared with the then calculated value ?


I don't know the details of the serialization format, but you can look them up if you're interested.

I would guess that the serialVersionUID is placed into the stream once per class, rather than with every object, but I don't know for sure. Other than that, yes, the serialVersionUID is packaged up with the objects we're serializing, unmarshalled at the other end, and compared to the ID for that class in the receiving environment.
 
Sreesanth Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Sreesanth Nair wrote:
Thanks Jeff. So, is it like when JVM serializes an object, the generated serialVersionUID is stored inside and when same file is deserialized, the inside value is compared with the then calculated value ?


I don't know the details of the serialization format, but you can look them up if you're interested.

I would guess that the serialVersionUID is placed into the stream once per class, rather than with every object, but I don't know for sure. Other than that, yes, the serialVersionUID is packaged up with the objects we're serializing, unmarshalled at the other end, and compared to the ID for that class in the receiving environment.



Just one more question Jeff. You had said that user can also define his own serialVersionUID. So, in that case, lets assume that I defined its value as 20L (long). Then, how at the receiving end, the JVM will come to know that it has to generate 20 and compare with this packaged one ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreesanth Nair wrote:
Just one more question Jeff. You had said that user can also define his own serialVersionUID. So, in that case, lets assume that I defined its value as 20L (long). Then, how at the receiving end, the JVM will come to know that it has to generate 20 and compare with this packaged one ?


If I'm gonig to deserialize a class, I have to have tha class in my classpath. Whoever wrote it must have given it to me. (Or I downloaded it from their site--same thing.)

Both the sender and receiver have that class in their classpath. You defined the class, set the UID, compiled it, and gave the jar to both sender and receiver. Later you updated your class, changed the UID to 21L. If you gave the updated jar to both sender and receiver, all is well. If you gave it only to the sender, then the receiever sees 21L but expected 20L (because that was in the older version of the class you gave him), so there's an error.
 
Sreesanth Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Sreesanth Nair wrote:
Just one more question Jeff. You had said that user can also define his own serialVersionUID. So, in that case, lets assume that I defined its value as 20L (long). Then, how at the receiving end, the JVM will come to know that it has to generate 20 and compare with this packaged one ?


If I'm gonig to deserialize a class, I have to have tha class in my classpath. Whoever wrote it must have given it to me. (Or I downloaded it from their site--same thing.)

Both the sender and receiver have that class in their classpath. You defined the class, set the UID, compiled it, and gave the jar to both sender and receiver. Later you updated your class, changed the UID to 21L. If you gave the updated jar to both sender and receiver, all is well. If you gave it only to the sender, then the receiever sees 21L but expected 20L (because that was in the older version of the class you gave him), so there's an error.



Ok. When you say the receiever sees 21L but expected 20L (because that was in the older version of the class you gave him) my question was how does the receiving JVM expect it to be 20L. During deserialization, I only have this class which has 21L as packaged serialVersionUID. So, how JVM gets to know that expected was 20 ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreesanth Nair wrote:So, how JVM gets to know that expected was 20 ?

Simply put: because it's part of the "output" of a serialized object.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreesanth Nair wrote:When you say the receiever sees 21L but expected 20L (because that was in the older version of the class you gave him) my question was how does the receiving JVM expect it to be 20L.


You gave him (or he downloaded) a version of your class that had the 20L that you set.

During deserialization, I only have this class which has 21L as packaged serialVersionUID. So, how JVM gets to know that expected was 20 ?


You provided both the sender and receiver with the 20L version. Then you made changes, updated the version to 21L, and provided that new version to the sender, but not to the receiver.

I write the class. I set it to 20L. I compile. I upload to myjavaclasses.com. The sender and receiver both download MyClass.class from there. Sender sends, receiver receives. All is well. I change my code. I set UID to 21L. I upload to myjavaclasses.com. Sender downloads the new MyClass.class. Sends it to receiver. He still has the old 20L version of MyClass.class in his classpath. He receives it. Sees that the class name is MyClass. So far so good, he says, I have that in my classpath, I know what that class is. But then as he unpacks it, he sees that the UID is 21L, but the MyClass in his classpath it 20L. ERROR!

Perhaps you were thinking that serialization packs up the entire class definition? That's not the case. It only sends the non-static field data and the UID. The deserializing JVM has to have the class already on his classpath.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked the same question at work recently Jeff. I think I answered correctly but I wish I had put it in the style/manner that you have here. Cheers for this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. Glad you found it useful!
 
Sreesanth Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff. That cleared all. Great explanation.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two recent discussions of SUIDs: 1 2. I think no 2 is better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!