Java Serialization
Java serialization is the process of converting an object into a byte stream, so that the object can be saved to disk, transferred over a network, or stored in memory, and later deserialized to convert the byte stream back into an object.
Serialization in Java is implemented through the java.io.Serializable interface, which has no methods and is merely a marker interface used to mark classes as serializable.
When you serialize an object, you package it into a special file that can be saved, transferred, or stored. Deserialization is opening this file, reading the serialized data, and then restoring it to an object for use in the program.
Serialization is a method for saving, transferring, and restoring objects; it enables objects to be moved and shared between different computers, which is very useful for distributed systems, data storage, and cross-platform communication.
The following are the basic concepts and usage of Java serialization:
Implement the Serializable interface: To make a class serializable, you need to have the class implement the java.io.Serializable interface, which tells the Java compiler that this class can be serialized; for example:
Example
import
java.io.Serializable
;
public
class
MyClass
implements
Serializable
{
// Class members and methods
}
**Serializing objects:** Use the ObjectOutputStream class to serialize an object into a byte stream; the following is a simple example:
Example
MyClass obj
=
new
MyClass
(
)
;
try
{
FileOutputStream
fileOut
=
new
FileOutputStream
(
"object.ser"
)
;
ObjectOutputStream
out
=
new
ObjectOutputStream
(
fileOut
)
;
out.
writeObject
(
obj
)
;
out.
close
(
)
;
fileOut.
close
(
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
The above code serializes the obj object into a file named "object.ser".
Deserializing objects: Use the ObjectInputStream class to deserialize an object from a byte stream; the following is a simple example:
Example
MyClass obj
=
null
;
try
{
FileInputStream
fileIn
=
new
FileInputStream
(
"object.ser"
)
;
ObjectInputStream
in
=
new
ObjectInputStream
(
fileIn
)
;
obj
=
(
MyClass
)
in.
readObject
(
)
;
in.
close
(
)
;
fileIn.
close
(
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
catch
(
ClassNotFoundException
e
)
{
e.
printStackTrace
(
)
;
}
The above code reads the byte stream from the "object.ser" file and deserializes it into a MyClass object.
The ObjectInputStream and ObjectOutputStream classes are high-level data streams that contain methods for deserializing and serializing objects.
The ObjectOutputStream class contains many write methods to write various data types, but one particular method is special:
public
final
void
writeObject
(
Object
x
)
throws
IOException
The above method serializes an object and sends it to the output stream. Similarly, the ObjectInputStream class contains the following method to deserialize an object:
public
final
Object
readObject
(
)
throws
IOException
,
ClassNotFoundException
This method takes the next object from the stream and deserializes it. Its return value is Object, so you need to cast it to the appropriate data type.
Example
To demonstrate how serialization works in Java, I will use the Employee class mentioned in the previous tutorial. Suppose we define the following Employee class, which implements the Serializable interface.
Employee.java File Code:
public
class
Employee
implements
java
.
io
.
Serializable
{
public
String
name
;
public
String
address
;
public
transient
int
SSN
;
public
int
number
;
public
void
mailCheck
(
)
{
System
.
out
.
println
(
"
Mailing a check to
"
name
"
"
address
)
;
}
}
Please note that for an object of a class to be serialized successfully, two conditions must be met:
The class must implement the java.io.Serializable interface.
All properties of the class must be serializable. If a property is not serializable, it must be marked as transient.
If you want to know whether a Java standard class is serializable, check the class's documentation. It is very simple to check whether an instance of a class can be serialized: just check whether the class implements the java.io.Serializable interface.
Serialize an object
The ObjectOutputStream class is used to serialize an object. The SerializeDemo example below instantiates an Employee object and serializes it to a file.
After the program runs, a file named employee.ser is created. The program produces no output, but you can understand what it does by studying the code.
Note: When serializing an object to a file, the Java convention is to give the file a .ser extension.
SerializeDemo.java file code:
import
java
.
io
.*;
public
class
SerializeDemo
{
public
static
void
main
(
String
[
]
args
)
{
Employee
e
=
new
Employee
(
)
;
e
.
name
=
"
Reyan Ali
"
;
e
.
address
=
"
Phokka Kuan, Ambehta Peer
"
;
e
.
SSN
=
11122333
;
e
.
number
=
101
;
try
{
FileOutputStream
fileOut
=
new
FileOutputStream
(
"
/tmp/employee.ser
"
)
;
ObjectOutputStream
out
=
new
ObjectOutputStream
(
fileOut
)
;
out
.
writeObject
(
e
)
;
out
.
close
(
)
;
fileOut
.
close
(
)
;
System
.
out
.
printf
(
"
Serialized data is saved in /tmp/employee.ser
"
)
;
}
catch
(
IOException
i
)
{
i
.
printStackTrace
(
)
;
}
}
}
Deserializing an Object
The DeserializeDemo program below demonstrates deserialization; /tmp/employee.ser stores the Employee object.
DeserializeDemo.java file code:
import
java
.
io
.*;
public
class
DeserializeDemo
{
public
static
void
main
(
String
[
]
args
)
{
Employee
e
=
null
;
try
{
FileInputStream
fileIn
=
new
FileInputStream
(
"
/tmp/employee.ser
"
)
;
ObjectInputStream
in
=
new
ObjectInputStream
(
fileIn
)
;
e
=
(
Employee
)
in
.
readObject
(
)
;
in
.
close
(
)
;
fileIn
.
close
(
)
;
}
catch
(
IOException
i
)
{
i
.
printStackTrace
(
)
;
return
;
}
catch
(
ClassNotFoundException
c
)
{
System
.
out
.
println
(
"
Employee class not found
"
)
;
c
.
printStackTrace
(
)
;
return
;
}
System
.
out
.
println
(
"
Deserialized Employee...
"
)
;
System
.
out
.
println
(
"
Name:
"
e
.
name
)
;
System
.
out
.
println
(
"
Address:
"
e
.
address
)
;
System
.
out
.
println
(
"
SSN:
"
e
.
SSN
)
;
System
.
out
.
println
(
"
Number:
"
e
.
number
)
;
}
}
The compilation and execution results of the above program are as follows:
Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101
Note the following key points:
The try/catch block in the readObject() method attempts to catch the ClassNotFoundException. For the JVM to deserialize an object, the class whose bytecode can be found must be available. If the JVM cannot find the class during deserialization, it throws a ClassNotFoundException.
Note that the return value of the readObject() method is cast to an Employee reference.
When the object is serialized, the SSN field has the value 111222333, but because the field is transient, that value is not sent to the output stream. Therefore, after deserialization the Employee object's SSN field is 0.