Always Set the serialVersionUID
serialVersionUID
is a class invariant that the RMI runtime uses to validate that the
classes on both sides of the wire are the same.
Here’s how it works: the first process marshals an
object and sends it over the wire. As part of the marshalling
process, the serialVersionUID of all relevant
classes is also sent. The receiving process compares the
serialVersionUIDs that were sent with the
serialVersionUIDs of the local classes. If they
aren’t equal, the RMI runtime will throw an instance
of
UnmarshalException
(and the method call will never even reach your code on the server
side).
If you don’t specify
serialVersionUID, you will run into two problems.
The first is that the system’s value for
serialVersionUID is generated at runtime (not at
compile time), and generating it can be expensive. The second, more
serious problem is that the automatically generated values of
serialVersionUID are created by hashing together
all the fields and methods of the class and are therefore
extraordinarily sensitive to minor changes. For these reasons,
whenever you define a class that will be marshalled, you should
always set the serialVersionUID, as in the
following example:
public class ClassWhichWillBeMarshalled implements Externalizable{
public static final long serialVersionUID = 1L;
// . . .
}