Friday, 26 August 2016

What happen if one of the members in the class doesn't implement Serializable interface?

If any of the members in the serializable class don’t implement Serializable interface, you will get ‘NotSerializableException’ while serializing the object.

Let me explain with an example.


Address.java
public class Address {
 private String street;
 private String city;

 public String getStreet() {
  return street;
 }

 public void setStreet(String street) {
  this.street = street;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 @Override
 public String toString() {
  return "Address [street=" + street + ", city=" + city + "]";
 }

}

import java.io.Serializable;

public class Employee implements Serializable {
 private static final long serialVersionUID = 1234L;

 private int id;
 private String firstName, lastName;
 private Address address;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public Address getAddress() {
  return address;
 }

 public void setAddress(Address address) {
  this.address = address;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", address=" + address
    + "]";
 }

}

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Test {
 private static ObjectOutputStream out;
 private static ObjectInputStream in;

 private static Employee getEmployee() {
  Employee emp = new Employee();

  emp.setFirstName("Hari Krishna");
  emp.setLastName("Gurram");
  emp.setId(1);

  Address address = new Address();
  address.setCity("Bangalore");
  address.setStreet("Marthalli");

  emp.setAddress(address);
  return emp;
 }

 public static void main(String args[]) throws IOException, ClassNotFoundException {
  /* Serialize object */
  FileOutputStream fos = new FileOutputStream("ser.out");
  out = new ObjectOutputStream(fos);
  Employee emp = getEmployee();
  out.writeObject(emp);

  /* Deserialize object */
  FileInputStream fis = new FileInputStream("ser.out");
  in = new ObjectInputStream(fis);
  Employee emp1 = (Employee) in.readObject();

  System.out.println(emp1);
 }
}


Since Address class doesn't implement Serializable interface, Java run time throws java.io.NotSerializableException. Try to run Test.java, you will end up in following exception.
Exception in thread "main" java.io.NotSerializableException: Address
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
 at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
 at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
 at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
 at Test.main(Test.java:31)


How to resolve above problem?
There are two options to resolve above problem.
a.   Make the Address field in Employee class transient. When you make the address variable as transient, java run time skips the serialization of address object. 
                   (OR)
b.   Let the Address class implement Serializable interface.

You may like



No comments:

Post a Comment