I am working on URLyBird. Using RMI. Can anybody clarify following doubts regarding generating serialVersionUid?
1. Is it mandatory to declare own serialVersionUid? or can we leave this job to compiler?
2. Can 2 serializable classes can have same serialVersionUID?
3. These IDs make sense only for objects which are transported over network right? (like only Remote objects..)
i couldn't find much info regd this in this forum. so starting a fresh topic. pl help.
A lot of information about serialVersionUID can be found here. After reading this article you should be able to answer your own questions.
The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.
1. I would declare it myself.
2. of course (all my classes in my assignment had the same serialVersionUID: 1979, my year of birth)
3. Every class that implements Serializable interface should declare a serialVersionUID.
Thanks a lot for your quick reply.
Yes. Things are much clear now. I too prefer to follow your technique.
But one need to know when to change the serialversionUid (as defined in specs). This makes it mandatory for junior programmers to understand the importance of this value whenever making changes to the class .
Anybody know if somebody lost points by only implements serialiVersionUID field in classes that will be transfer by network ?
Well, if the class is going to be transfered by network, then it must implement java.io.Serializable, and that's the only case you need the serialVersionUID. If you don't explicitly create one, then the compiler will create one for you. This is related to the version of the class. Whenever I create a class that implements Serializable, I always declare this field.
If you use the -Xlint:serial when compiling your source code, then it will tell you which classes needs or inherit serialVersionUID. Most Swing subclasses, serializable classes and exception classes need one.
... extending from JFrame ... will lead to [a requirement for] defining a serialVersionUID.
I would question whether anyone needs to extend from JFrame for this assignment. Are you truly creating a new class that is aJFrame to be used by other classes, or would it be more true to say that your GUI has aJFrame?