Serializing an object with static property

Serializing an object with static property

Serialization is often used to convert the in memory objects to stream and vice-versa. But static members are not part the object states so they are not serialized in this process.

You need to serialize an object that has some static properties. The answer is, you need to control the process of serialization and De-Serialization for those objects. To do this in java you need to implement the interface Externalizable. This is similar to the Serializable interface but with more control over the process of the serialization.

java.io.Externalizable interface provides two methods writeExternal() and readExternal(). These methods are invoked whenever an object is being serialized or De-serialized. Syntax of both are shown below.

void writeExternal(ObjectOutput out) throws IOException;
void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;

For demonstration, I have created a class ExternalizableEmployee which has only two Strong fields, name and a static field company.

Approach

As we know that serialization will not save a property marked as static. we can create a proxy object that has the same properties but none of them is marked as static. So when you save the proxy object it will save all the properties and vice-versa when reading. In this example, I created a static Employee class with the ExternalizableEmployee class. Within because we do not need this outside the scope of ExternalizableEmployee class.

ExternalizableEmployee class

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

public class ExternalizableEmployee implements Externalizable {

	private String name;
	private static String company;

	// Inner Proxy class for Serialization.
	private static class Employee implements Serializable {
		private static final long serialVersionUID = 610993458650534361L;

		String name;
		String company;

		public Employee(String name, String company) {
			super();
			this.name = name;
			this.company = company;
		}

		public String getName() {
			return name;
		}

		public String getCompany() {
			return company;
		}

	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		/*
		 * Write the object of proxy class Employee with the values of
		 * ExternalizableEmployee
		 */
		Employee emp = new Employee(name, company);
		out.writeObject(emp);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		/* Read the proxy object and initialize ExternalizableEmployee */
		Employee employee = (Employee) in.readObject();
		this.name = employee.getName();
		company = employee.getCompany();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static String getCompany() {
		return company;
	}

	public static void setCompany(String company) {
		ExternalizableEmployee.company = company;
	}

}

Write ExternalizableEmployee object to File

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

public class WriteExternalizableEmployeeTest {
	public static void main(String[] args) {
		try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("employee.data"))) {
			// create an object of ExternalizableEmployee
			ExternalizableEmployee emp=new ExternalizableEmployee();
			emp.setName("Test");
			ExternalizableEmployee.setCompany("ByteFold");
			
			// write object to file
			objectOutputStream.writeObject(emp);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Read ExternalizableEmployee object

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

public class ReadExternalizableEmployeeTest {
	public static void main(String[] args) {
		try (ObjectInputStream objectOutputStream = new ObjectInputStream(new FileInputStream("employee.data"))) {
			// Read an object of ExternalizableEmployee
			ExternalizableEmployee employee = (ExternalizableEmployee) objectOutputStream.readObject();

			System.out.println(employee.getName());
			System.out.println(employee.getCompany());
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

Find the complete code at GitHub

More about Serialization

Leave a Reply

Your email address will not be published. Required fields are marked *