GoConqr has a new look! Let us know what you think.

Mind map

Serialization

Abdelkrim ZAHMOUNI English

Map content

Serialization

  1. 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
  2. Object does NOT implement Serializable

  3. Class B extends A and A is Serializable => B is Serializable

  4. 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
  5. collection interfaces are not serializable but concrete collection classes in java API are

  6. 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

  7. 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
    • Not for statics
    • Collection/Array: every element must be serializable

Information

Description
Mind Map on Serialization, created by Abdelkrim ZAHMOUNI on 04/10/2018.
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

Feedback

What is it about?

What do you think of the new look?

How do you like it?