Understanding Java Serialization, Deserialization, and serialVersionUID
This article explains Java object serialization and deserialization, why implementing Serializable and defining a serialVersionUID are necessary for persistence and network transfer, and demonstrates the impact of transient and static fields with complete code examples.
When a Java application needs to persist objects to disk, send them over the network, or interact with a browser, serialization converts an object to a byte stream and deserialization restores it.
Implementing java.io.Serializable enables the JVM to handle this automatically; otherwise custom code is required. The serialVersionUID field identifies compatible class versions. If omitted, the JVM generates one, which can cause InvalidClassException after class changes.
Specifying a constant serialVersionUID prevents version‑mismatch errors. The article provides a full example with a User class and a test program that serializes and deserializes the object, showing the effect of adding a new field without a declared serialVersionUID :
public class User implements Serializable {
private String name;
private Integer age;
// no serialVersionUID
// getters, setters, toString()
}
public class SerializableTest {
private static void serialize(User user) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\111.txt"));
oos.writeObject(user);
oos.close();
}
private static User deserialize() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\111.txt"));
return (User) ois.readObject();
}
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("tyshawn");
user.setAge(18);
System.out.println("Before serialization: " + user);
serialize(user);
User dUser = deserialize();
System.out.println("After deserialization: " + dUser);
}
}After adding a declared private static final long serialVersionUID = 1L; to User , the same test runs without errors even when the class definition changes (e.g., adding a new field).
Additional properties such as transient fields and static fields are not serialized. The article shows a second example where a transient String sex and a static String signature are excluded from the serialized form, confirming their behavior:
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private Integer age;
private transient String sex;
private static String signature = "default";
// getters, setters, toString()
}The deserialization result shows that sex becomes null and signature retains the current static value, illustrating why transient and static members are ignored during serialization.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.