File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes serialVersionUID  in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "serialVersionUID  in java" Watch "serialVersionUID  in java" New topic

serialVersionUID in java

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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.

Tanu Gulati
Fyodor Sherstobitov

Joined: Feb 21, 2011
Posts: 9
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

Joined: Dec 06, 2010
Posts: 448

Even you get warning from compiler if you don't have serialVersionUID in a class implementing the Serializable interface.

Java Experience
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: serialVersionUID in java
jQuery in Action, 3rd edition