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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

SerializationinJava_1

SerializationinJava_2

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.

DeserializationinJava_1

DeserializationinJava_2

DeserializationinJava_3

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

IS-ARelationship_1

IS-ARelationship_2

IS-ARelationship_3

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.

HAS-ARelationship

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.

StaticDataMember

StaticDataMember_rslt

Become a Certified UI UX Expert in Just 5 Months!

UMass Amherst UI UX BootcampExplore Program
Become a Certified UI UX Expert in Just 5 Months!

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.

TransientKeyword_1

TransientKeyword_2

TransientKeyword_3

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.

Happy learning!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449