What Changes are Compatible and Incompatible to the Mechanism of Java Serialization

This is one of a difficult and tricky questions and answering this correctly would mean you are an expert in Java Serialization concept. In an already serialized object, the most challenging task is to change the structure of a class when a new field is added or removed. As per the specifications of Java Serialization, … Read more

How can a Sub-Class of Serializable Super Class Avoid Serialization?

How can a Sub-Class of Serializable Super Class Avoid Serialization? If Serializable Interface is Implemented by the Super Class of a Class, how can the Serialization of the Class be Avoided? In Java, if the super class of a class is implementing Serializable interface, it means that it is already serializable. Since, an interface cannot … Read more

Is it Possible to Customize the Serialization Process? How can we Customize the Serialization Process

Yes, the serialization process can be customized. When an object is serialized, objectOutputStream.writeObject (to save this object) is invoked and when an object is read, ObjectInputStream.readObject () is invoked. What most people do not know is that Java Virtual Machine provides you with an option to define these methods as per your needs. Once this … Read more

How to Serialize a Collection in Java? How to Serialize a ArrayList, Hashmap or Hashset object in Java

All standard implementations of collections List, Set and Map interface already implement java.io.Serializable. All the commonly used collection classes like java.util.ArrayList, java.util.Vector, java.util.Hashmap, java.util.Hashtable, java.util.HashSet, java.util.TreeSet do implement Serializable. This means you do not really need to write anything specific to serialize collection objects. However you should keep following things in mind before you serialize … Read more

Why does Serialization NOT Save the Value of Static Class Attributes? Why Static Variables are not Serialized

The Java variables declared as static are not considered part of the state of an object since they are shared by all instances of that class. Saving static variables with each serialized object would have following problems It will make redundant copy of same variable in multiple objects which makes it in-efficient. The static variable … Read more