Site icon

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

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

1. Static data member:

2. Transient data member:

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: 

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

 

Exit mobile version