This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java class consisting of serialVersionUID

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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


Thanks in advance
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://tinyurl.com/ycqoucu
 
shanta raja
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic