Understanding Java Serialization: Concepts, Implementation, and Common Pitfalls
This article explains Java serialization and deserialization concepts, when to use them, how to implement the Serializable interface with example code, and discusses the effects of transient, static fields, and serialVersionUID on object persistence and compatibility.
Java serialization allows converting objects to byte streams for storage or transmission, and deserialization restores them.
The article explains the concepts of serialization and deserialization, when they are needed (file storage, sockets, RMI), and how to enable them by implementing the java.io.Serializable interface.
Sample code demonstrates a FlyPig model implementing Serializable , showing fields, a static variable, a transient field, and a custom toString method:
package com.lxk.model; import java.io.Serializable; /** * @author lxk on 2017/11/1 */ public class FlyPig implements Serializable { //private static final long serialVersionUID = 1L; private static String AGE = "269"; private String name; private String color; transient private String car; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getCar() { return car; } public void setCar(String car) { this.car = car; } @Override public String toString() { return "FlyPig{" + "name='" + name + '\'' + ", color='" + color + '\'' + ", car='" + car + '\'' + ", AGE='" + AGE + '\'' + '}'; } }
A test class serializes a FlyPig instance to d:/flyPig.txt using ObjectOutputStream , then deserializes it with ObjectInputStream , illustrating that transient fields are not persisted and static fields are not serialized:
package com.lxk.test; import com.lxk.model.FlyPig; import java.io.*; /** * Serialization test */ public class SerializableTest { public static void main(String[] args) throws Exception { serializeFlyPig(); FlyPig flyPig = deserializeFlyPig(); System.out.println(flyPig.toString()); } private static void serializeFlyPig() throws IOException { FlyPig flyPig = new FlyPig(); flyPig.setColor("black"); flyPig.setName("naruto"); flyPig.setCar("0000"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:/flyPig.txt"))); oos.writeObject(flyPig); System.out.println("FlyPig 对象序列化成功!"); oos.close(); } private static FlyPig deserializeFlyPig() throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:/flyPig.txt"))); FlyPig person = (FlyPig) ois.readObject(); System.out.println("FlyPig 对象反序列化成功!"); return person; } }
Experiments modify the static AGE value after serialization and observe that it is not restored from the file, confirming static fields are excluded from serialization.
The role of serialVersionUID is discussed; omitting it can cause InvalidClassException when class definitions change, so defining a constant long value (e.g., 1L ) is recommended. Example of the exception:
InvalidClassException: com.lxk.model.FlyPig; local class incompatible: stream classdesc serialVersionUID = -3983502914954951240, local class serialVersionUID = 7565838717623951575
Overall, the guide provides practical insights into Java serialization pitfalls, proper use of serialVersionUID , and best practices for maintaining compatibility across versions.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.