Serialization Basics
Understand the basics of Java serialization: Serializable interface, ObjectOutputStream, and ObjectInputStream.
Serialization Basics is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Serialization means saving an object's state to a file (or stream) and restoring it later. It is often used for persistence or sending objects.
Serializable interface
A class must implement java.io.Serializable to allow its objects to be serialized. This is a marker interface with no methods.
Code: Serializable class
Here Person implements Serializable. This makes it eligible for writing with ObjectOutputStream.
import java.io.Serializable;
public class Main {
static class Person implements Serializable {
String name;
int age;
Person(String n, int a) { name = n; age = a; }
public String toString() { return name + "(" + age + ")"; }
}
public static void main(String[] args) {
Person p = new Person("Ada", 30);
System.out.println("Created: " + p);
}
}
ObjectOutputStream
ObjectOutputStream writes an object to a file. The file will contain a binary form of the object.
Code: write object
This writes a Person object to a file named person.ser.
import java.io.*;
public class Main {
static class Person implements Serializable {
String name; int age;
Person(String n,int a){name=n;age=a;}
}
public static void main(String[] args) {
Person p = new Person("Bob",25);
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
out.writeObject(p);
System.out.println("Object written");
} catch(IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Code: read object
This reads the Person object back from the file and prints it.
import java.io.*;
public class Main {
static class Person implements Serializable {
String name; int age;
Person(String n,int a){name=n;age=a;}
public String toString(){return name+"("+age+")";}
}
public static void main(String[] args) {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) {
Person p = (Person) in.readObject();
System.out.println("Read: " + p);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Serialization check
Quick check: What must a class do to be serializable?
Recap
Recap: Serialization saves objects with ObjectOutputStream and restores them with ObjectInputStream. Classes must implement Serializable.
Frequently asked questions
Is the “Serialization Basics” lesson free?
Yes — the full text of “Serialization Basics” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Serialization Basics”?
Understand the basics of Java serialization: Serializable interface, ObjectOutputStream, and ObjectInputStream. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Serialization Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Serialization Basics
- Simple CSV/JSON Handling
- File Walking & Utilities