Last Updated on 29 July 2019   |   Print Email
Serialization is the process of saving an object’s states in a persistent format (such as file stream or database), and later restoring them back from the stream (de-serialization). In Java, an object of a class is serializable if the class implements the java.io.Serializable interface. This is a marker interface which tells the JVM that the class is eligible for serialization. In this article, we’ll learn how to serialize and de-serialize a simple Java object.First, let’s create an object model class (User.java) as follows:
package net.codejava.io;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private static final long serialVersionUID = 1234L;
private String username;
private String email;
private transient String password;
private Date birthday;
private int age;
public User(String username, String email, String password, Date birthday,
int age) {
this.username = username;
this.email = email;
this.password = password;
this.birthday = birthday;
this.age = age;
}
public void printInfo() {
System.out.println("username: " + username);
System.out.println("email: " + email);
System.out.println("password: " + password);
System.out.println("birthday: " + birthday);
System.out.println("age: " + age);
}
// getters and setters
}
There are three important points in this model class:
It must implements the Serializable interface. Otherwise, we’ll get a java.io.NotSerializableException when trying to serialize an object of the class.
A constant named serialVersionUID is declared and assigned a long value:
private static final long serialVersionUID = 1234L;
This is a conventional constant which should be declared when a class implements the Serializable interface. The serial version UID strongly ensures compatibility between the serialized and de-serialized versions of objects of a class, because the process of serialization and de-serialization can happen on different computers and systems. Although this declaration is optional, it’s always recommended to declare the serialVersionUID for a serializable class.
Notice that the password field is marked as transient:
private transient String password;
Because we don’t want store the password when serializing the object. The rule is, when a variable is marked as transient, its object won’t be serialized during serialization.
Now, let’s look at some examples of how to serialize an object of the above User class to a file stream, and then de-serialize it from the file.
Java Serialization Example:
The following method serializes an object of type User to a file stream specified by the given filePath:
As we see, all the fields of the User object are saved and restored back perfectly, except the password field is null, because we mark it as transient.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
I am getting an error "System cannot find the path specified " I used the full directory path of the file with the file name and ext. as an argument for FileOutputStream I am using NetBeans IDE 8.2
Comments
I used the full directory path of the file with the file name and ext. as an argument for FileOutputStream
I am using NetBeans IDE 8.2