• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java class consisting of serialVersionUID

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

What advantage we get if the java class have a serialVersionUID??


Thanks in advance
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://tinyurl.com/ycqoucu
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/javase/6/docs/platform/serialization/spec/version.html
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is said that declaration of serialVersionUID is highly recommended, but why is the declaration of the method getSerialVersionUID() not made mandatory? In the absence of this method, the error message 'isSerialVersionUID' is displayed.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SearchFirst ! You've been posting a lot of questions which can be easily answered via a Google search.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shanta raja wrote:It is said that declaration of serialVersionUID is highly recommended, but why is the declaration of the method getSerialVersionUID() not made mandatory? In the absence of this method, the error message 'isSerialVersionUID' is displayed.


Because Serializable doesn't require such a method--that's just the way it was originally defined (and there's no really good reason to change that I can think of, although I suppose I could envision a scenario where I might want to use it without digging for it).
 
shanta raja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am aware of the importance of serialVersionUID from the docs in sun website and google searches. My question still remains unanswered, maybe I was not clear. It is mentioned that the basic purpose of the serialVersionUID field is to allow the JVM to compare incoming serialized objects to the local definition of the class, to ensure that no changes have occurred that may affect serialization/deserialization. When JVM tries to compare incoming serialized objects and does not find a corresponding getSerialVersionUID method defined in the class declaration, then it throws an error. In the absence of the getSerialVersionUID method, JVM calculates the serialVersionUID using hashcodes and then compares the serialized objects. What is the comparison method used? The JVM must be using some getSerialVersionUID method declared elsewhere. Is the getSerialVersionUID method from the ObjectStreamClass?. What I mean to say is if you write your own serialVersionUID, then you need to override the getSerialVersionUID method, just like the equals and hashcode methods.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shanta raja wrote:When JVM tries to compare incoming serialized objects and does not find a corresponding getSerialVersionUID method defined in the class declaration, then it throws an error.


None of my JVMs do.

What I mean to say is if you write your own serialVersionUID, then you need to override the getSerialVersionUID method, just like the equals and hashcode methods.


I don't believe that's true: the following works just fine (lifted from Wikipedia, I think):(Line 39 is commented out because I changed the serialVersionUID from 42L to 43L and tried to read in the previously-serialized object, it failed because the serialVersionUID wasn't the same as when I serialized it originally.)

Is there something you're reading or studying that makes you believe you need this method that isn't defined in the Serializable interface?
 
shanta raja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, its like this. I added serialVersionUID to all serializable classes, but later got problems while trying to compare the objects. The code first tries to get all fields of the class and their respective accessors. Since it could not find a getSerialVersionUID declared, the code failed.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were using the serialVersionUID as part of your compare, and wrote your code to expect a getter for it? I see.

I'm just trying to avoid any confusion: the only thing that's necessary for Serializable to work is a serialVersionID property--no getter is required for implementing or using Serializable.
 
shanta raja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I agree there is no need to have an accessor method for serialversionuid. I am not the designer of the application, but maintain it. I wasn't aware that somewhere objects were being compared. I just declared the serialversionuid just like its recommended. But had to face problems while using the application.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic