• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialized Objects

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting a error that says incompatible serial ID. What does that mean and why am I getting it? General thoughts and responses about serialized objects and errors associated with them are what I am looking for.
Thanks,
Paul
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
youusually get that error if you change an interface and dont recompile the whole application
if you just recompile the item you changed
the method that tries to call it is looking for a diffrent ID
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java serialisation stores, in the stream, an ObjectStreamClass for each object that has been serialised. This includes, among other things, the name and the serialisation unique identifier (serialVersionUID) of the class.
When deserialising, the serialisation unique identifier must be the same in the stream being serialised as it is in the class of the same name that is loaded. If not, you will get InvalidClassException.
By default, a Serializable class gets a serialisation unique identifier assigned to it by Java. This is obtained by doing some sort of hashing of the data fields and the method signatures in the class, I think.
However, you can specify your own value for serialisation unique identifier, by a line like the following in your class:
static final long serialVersionUID = 7660679424232768991L;
Why would you want to do that?
Well, the default value changes whenever you make any change to the fields or methods of the class. However, changes to the methods may very well not mean that it is wrong to deserialise an old serialised object using the new class. By specifying your own value, and not changing it when you change the methods, you can say to Java that it's OK to deserialise the old serialised object.
Further, Java serialisation is pretty smart in that it stores the field names. So, in fact, if you add a new field to a class, and give an appropriate default value, it may still be OK to deserialise the old object; the field that is missing will simply be defaulted. Again, if you want this to happen, specify your own serialVersionUID value and don't change it when you add the field.
If you delete fields or change their types or their meanings, it is probably not a good idea to try to deserialise an old object.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic