• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serialVersionUID in java

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was reading an article today and it said it is important to give serialVersionUID variable to your class which we think will be required to serialize. My question are given below
1.) what will be the problem we will face if we do not give serialVersionUID in our serialized java class (i know java will create one for us if we dont give but what is the actual significance)
2.) I got to know that serialVersionUID should be unique in our class and there is a serialVer utility from java which create a unique number for every class. now my question is i give every serialized class the same serialVersionUID, what problems we will face during serialization and de-serialization process.

 
Fyodor Sherstobitov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
serialVersionUID is used by Java Serialization mechanism to detect if the target object was modified during serialization/deserialization process. If you will try to cast deserialized object to it's original type and JVM will see, that serialVersionUID differs in class definition and in inctance variable of an object, it will cast InvalidClassExceptions.
The problem with autogenerated serialVersionUID is that it is being generated on the base of some iformation about the class that JVM has and may differ from one version of JVM to another, so you can have collisions serializing your objects on one system and trying to deserialize them on other.
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even you get warning from compiler if you don't have serialVersionUID in a class implementing the Serializable interface.
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily. I've never had that warning when compiling from the command line. In Eclipse I do get that warning, but you can turn it off if you want. I believe the default is off, but I can't remember.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic