- Serialization in Java [Advantages and Examples Explained]
- What is Serialization in Java?
- What are the Advantages of Serialization?
- Points to Note About Serialization in Java?
- Here’s How to Land a Top Software Developer Job
- How to Serialize an Object?
- Syntax for the writeObject() method:
- Syntax for the readObject() method:
- Example for Serialization in Java
- Example for Deserialization in Java
- Basics to Advanced — Learn It All!
- Serialization in Java With Inheritance (Is-A Relationship)
- Serialization in Java With Aggregation (Has-A Relationship)
- Serialization in Java with Static Data Member
- Kickstart Your UI/UX Career Right Here!
- What is the Transient Keyword?
- Conclusion
- About the Author
- Recommended Programs
- Serialization and Deserialization in Java with Example
Serialization in Java [Advantages and Examples Explained]
This serialization in Java article will shed light on the mechanism of serialization, and its benefits. It will also explore how to serialize an object, and how to serialize using different Java concepts, with examples.
What is Serialization in Java?
Serialization in Java is the concept of representing an object’s state as a byte stream. The byte stream has all the information about the object. Usually used in Hibernate, JMS, JPA, and EJB, serialization in Java helps transport the code from one JVM to another and then de-serialize it there.
Deserialization is the exact opposite process of serialization where the byte data type stream is converted back to an object in the memory. The best part about these mechanisms is that both are JVM-independent, meaning you serialize on one JVM and de-serialize on another.
What are the Advantages of Serialization?
Serialization offers a plethora of benefits. Some of its primary advantages are:
- Used for marshaling (traveling the state of an object on the network)
- To persist or save an object’s state
- JVM independent
- Easy to understand and customize
Points to Note About Serialization in Java?
To serialize an object, there are a few conditions to be met. Some other key points need to be highlighted before you proceed further in the article. These are the conditions and points to remember while using serialization in Java.
- Serialization is a marker interface with no method or data member
- You can serialize an object only by implementing the serializable interface
- All the fields of a class must be serializable; otherwise, use the transient keyword (more about it later)
- The child class doesn’t have to implement the Serializable interface, if the parent class does
- The serialization process only saves non-static data members, but not static or transient data members
- By default, the String and all wrapper classes implement the Serializable interface
Here’s How to Land a Top Software Developer Job
How to Serialize an Object?
Since you now know what serialization in Java is, and all the relevant points, let’s delve deep into how to serialize an object. You must use the writeObject() method of the ObjectOutputStream class for serialization and readObject() method of the InputObjectStream class for deserialization purpose.
Syntax for the writeObject() method:
public final void writeObject(Object o) throws IO Exception
Syntax for the readObject() method:
public final Object readObject() throws IOException, ClassNotFoundException
Let’s look at an example to understand how to serialize and de-serialize an object in Java.
Example for Serialization in Java
The following program code will serialize a student object and save it to a file named student.ser.
Example for Deserialization in Java
In the code below, you will look at how to deserialize the student object that was have serialized in the above example.
Basics to Advanced — Learn It All!
Serialization in Java With Inheritance (Is-A Relationship)
As mentioned in the points to note section, when a parent class implements the Serializable interface, the child classes do not have to do so. Let’s look at that point in action with this Java serialization example in inheritance.
Serialization in Java With Aggregation (Has-A Relationship)
In the below code, the Student object will not be serialized as the Address class does not implement the Serializable interface, and it has tried to use the reference from the Address class to serialize the Student class. This means that if you want to serialize an object with reference to another class, all the references must be serializable, or else it will throw NotSerializableException.
Serialization in Java with Static Data Member
Serialization ignores the static data members in Java. In the example below, you will use a static data member and serialize the object. But since the serialization will have no impact on the static data, its value would be changeable before de-serializing it.
Kickstart Your UI/UX Career Right Here!
What is the Transient Keyword?
The transient is a reserved keyword in Java. If you don’t want any data member to get serialized, you have to add the transient keyword. In the below program code, you will use the transient keyword to two data members and display all the data members’ values.
As you can see, the transient data members return the default value, which is 0 for integers and null for strings.
Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.
Conclusion
In this article, you learned everything about serialization in Java. If you want to learn more advanced concepts, you can opt for Simplilearn’s Online Java Certification Course. The course will help you grasp several Java programming concepts and apply them to Hibernate, Spring, and other frameworks.
Have any questions for us regarding Serialization in Java? Leave them in the comments section of this article, and our experts will get back to you ASAP.
About the Author
Simplilearn
Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.
Recommended Programs
Full Stack Java Developer Job Guarantee Program
Serialization and Deserialization in Java with Example
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform. To make a Java object serializable we implement the java.io.Serializable interface. The ObjectOutputStream class contains writeObject() method for serializing an Object.
public final void writeObject(Object obj) throws IOException
The ObjectInputStream class contains readObject() method for deserializing an object.
public final Object readObject() throws IOException, ClassNotFoundException
Advantages of Serialization
Only the objects of those classes can be serialized which are implementing java.io.Serializable interface. Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and Remote.
Points to remember
1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true. 2. Only non-static data members are saved via Serialization process.
3. Static data members and transient data members are not saved via Serialization process. So, if you don’t want to save value of a non-static data member then make it transient.
4. Constructor of object is never called when an object is deserialized.
5. Associated objects must be implementing Serializable interface. Example :
class A implements Serializable< // B also implements Serializable // interface. B ob=new B(); >
SerialVersionUID The Serialization runtime associates a version number with each Serializable class called a SerialVersionUID, which is used during Deserialization to verify that sender and receiver of a serialized object have loaded classes for that object which are compatible with respect to serialization. If the receiver has loaded a class for the object that has different UID than that of corresponding sender’s class, the Deserialization will result in an InvalidClassException.
A Serializable class can declare its own UID explicitly by declaring a field name. It must be static, final and of type long. i.e- ANY-ACCESS-MODIFIER static final long serialVersionUID=42L; If a serializable class doesn’t explicitly declare a serialVersionUID, then the serialization runtime will calculate a default one for that class based on various aspects of class, as described in Java Object Serialization Specification. However it is strongly recommended that all serializable classes explicitly declare serialVersionUID value, since its computation is highly sensitive to class details that may vary depending on compiler implementations, any change in class or using different id may affect the serialized data. It is also recommended to use private modifier for UID since it is not useful as inherited member. serialver The serialver is a tool that comes with JDK. It is used to get serialVersionUID number for Java classes.
You can run the following command to get serialVersionUID serialver [-classpath classpath] [-show] [classname…] Example 1: