Impact of static & transient data member on serialization in java (example)

  • Serialization is the process of saving the objects loaded in the memory to corresponding streams.
    • e.g Save the objects to a file system (Layman term save object to a file).
  • Deserialization is the process of loading the saved data to corresponding objects.
  • We have discussed about the serialization deserialization in java.

Impact of Static & transient data members on serialization in java:

1. Static data member:

  • Static variables are not part of object.
  • When we serialize the object, the static variable(s) are not be serialized.

2. Transient data member:

  • Sometimes, we do not want serialize some data members.
    • Then, we can mark those variables as transient variables.
  • Transient data members are not be serialized.
  • When, we deserialize objects, the transient variable(s) will have default values.
    • e.g if integer is marked as transient, after deserialization it will initialized to 0.

Program:Impact of Static & transient data members in java

1. Person Class (or POJO class):

  1. Given an Person class, we would like to serialize Person object(s).
  2. Person class will contain data members like firstName, lastName, age & contact.
  3. We will mark lastName as static variable.
  4. We will mark contact as transient variable.
  5. Static variable(s) are not part of object.
    • lastName will never be serialized.
  6. Transient variables are not be serialized.
    • contact data member will not be serialized.
package org.learn;

import java.io.Serializable;

public class Person implements Serializable {

	private static final long serialVersionUID = 1L;
	public String firstName;
	public static String lastName = "Static member will not be serialzed";
	public int age;
	public transient String contact = "Trasient member will not be serialized";

	public Person(String firstName, int age, String contact) {
		this.firstName = firstName;
		this.age = age;
		this.contact = contact;
	}

	public String toString() {
		return "[" + firstName + " " + lastName + " " + age + " " + contact + "]";
	}
}

2. Serialize & Deserialize method: 

  • serialize method will serialize the person object
    1. lastName is static data member, hence lastName will not serialized.
    2. We will modify static variable (lastName) after person object is serialized.
    3. When, we deserialize the person object.
      • Person Object will have modified value, which we have set after serialization.
      • Hecne, static members are not serialized.
  • Also, contact data member will not be serialized, as it is transient.
    • When, we deserialize the contact data member, it will get null value.
package org.learn;

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

public class SerializeDeserialize {

	public static void serialize() throws IOException {
		Person person = new Person("Mike", 34, "001894536");
		// Set static variable
		Person.lastName = "harvey";
		FileOutputStream output = new FileOutputStream(new File("savePerson.txt"));
		ObjectOutputStream outputStream = new ObjectOutputStream(output);
		outputStream.writeObject(person);
		outputStream.flush();
		outputStream.close();
		System.out.println("Serialized the person object : " + person);

		// Update the static variable to confirm that its not serialized
		Person.lastName = "Setting last name after serialization";
	}

	public static void deSerialize() throws IOException, ClassNotFoundException {

		FileInputStream inputStream = new FileInputStream(new File("savePerson.txt"));
		ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
		Person person = (Person) objectInputStream.readObject();
		System.out.println("Deserialize the person object :" + person);
		inputStream.close();
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		serialize();
		deSerialize();
	}
}

Output – Static & transient data members during serialization in java

Serialized the person object : [Mike harvey 34 001894536]
Deserialize the person object :[Mike Setting last name after serialization 34 null]

 

Download Code – Serialization Deserialization in java

 

Scroll to Top