Mind map
Serialization
Map content
Serialization
Save this object and all of its non transient instance variables
- new ObjectOutputStream(new FileOutputStream("f.out")).writeObject(Object) // serialize and write
- NotSerializableException if Object non transiant graphs are not all serializable
- new ObjectInputStream(new FileInputStream("f.in")).readObject() // read and deserialize
- private methods to implement, if present, will be invoked automatically during serialization and deserialization
- private void writeObject(ObjectOutputStream os) { }
- private void readObject(ObjectInputStream is) { }
- os.defaultWriteObject(); & is.defaultReadObject(); represent the normal serialization process for this object
- os.writeInt(theCollar.getCollarSize()); theCollar = new Collar(is.readInt());
- I/O-related methods writeObject() can throw exceptions. You can declare them or handle them but we recommend handling them.
- Read the extra stuff in the same order you wrote it
- common reason: save some part of an object's state manually
- new ObjectOutputStream(new FileOutputStream("f.out")).writeObject(Object) // serialize and write
Object does NOT implement Serializable
Class B extends A and A is Serializable => B is Serializable
Deserialization process
- The constructor does not run, and instance variables are NOT given their initially assigned values
- Transient instance variables get the default value according to their types
- Instance variables INHERITED from NOT serializable classe are reset to the values they were given during the original construction of the object: the nonserializable class no-argument constructor WILL run, if no such constructor InvalidClassException
collection interfaces are not serializable but concrete collection classes in java API are
If you serialize an object using one version of the class, but attempt to deserialize it using a newer, different version of the class, deserialization might fail
Rules
- Object Graphs (dependencies hierarchy) should all be serializable; Otherwise transient keyword should be used to skip instance variables of non serializable types
- We can subclass but it is not always an option
- The class might be final
- The class might itself refer to other non-serializable objects, and without knowing the internal structure
- Other reasons related to your design
- We can subclass but it is not always an option
- Not for statics
- Collection/Array: every element must be serializable
- Object Graphs (dependencies hierarchy) should all be serializable; Otherwise transient keyword should be used to skip instance variables of non serializable types
Information
- Description
- Mind Map on Serialization, created by Abdelkrim ZAHMOUNI on 04/10/2018.
- Created by
- Abdelkrim ZAHMOUNI
- Views
- 0
- History
-
- Created by Abdelkrim ZAHMOUNI · Sep 23, 2018
- Copied by Abdelkrim ZAHMOUNI · Oct 3, 2018
- Copied by Abdelkrim ZAHMOUNI · Oct 4, 2018
- Rating