What advantage we get if the java class have a serialVersionUID??
Thanks in advance
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 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?
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.