Search code examples
javaclassstaticserializableserialversionuid

How is serialVersionUID serialized in Java?


Class members (static) cannot be serialized. The reason is obvious - they are not held by the object(s) of the class. Since they are associated with the class (rather than the object of that class), they are stored separately from the object.

serialVersionUID is declared as a static field within a class that implements the java.io.Serializable interface something like the following.

private static final long serialVersionUID = 1L;

It is used as a version control in a Serializable class. If it is not explicitly declared, will be done automatically by JVM, based on various aspects of the Serializable class, as described by the Java(TM) Object Serialization Specification.

If it is not explicitly declared within the class implementing the Serializable interface then a warning may issue.

The serializable class SomeClass does not declare a static final serialVersionUID field of type long

Is it serialized even though it is static, how or is it an exception to serialization?


Solution

  • Serialization is done "magically," with lots of reflection, and has all sorts of special behavior -- including e.g. looking up the static serialVersionUID of the class.