GeeCON Prague 2014*
The moose likes Java in General and the fly likes Java class consisting of serialVersionUID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Java class consisting of serialVersionUID " Watch "Java class consisting of serialVersionUID " New topic
Author

Java class consisting of serialVersionUID

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi

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


Thanks in advance


Save India From Corruption - Anna Hazare.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://tinyurl.com/ycqoucu
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

http://java.sun.com/javase/6/docs/platform/serialization/spec/version.html


http://muhammadkhojaye.blogspot.com/
shanta raja
Greenhorn

Joined: Apr 01, 2010
Posts: 8
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

SearchFirst ! You've been posting a lot of questions which can be easily answered via a Google search.


[My Blog]
All roads lead to JavaRanch
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 01, 2010
Posts: 8
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 01, 2010
Posts: 8
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 01, 2010
Posts: 8
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.
 
GeeCON Prague 2014
 
subject: Java class consisting of serialVersionUID