1 2 Previous Next 18 Replies Latest reply on Aug 16, 2010 3:28 PM by 843790

    Understand the overriding of writeObject() and readObject() to manually


      I am trying to understand when to use overriding of writeObject() and readObject() methds and when ObjectStreamField.

      It will be good if any one can explain it me with example or programming scenario :)

      Below I have two classes:

      MathOperation: non-serialisable
      SerializeMathOperationClass: non-serialisable

      MathOperation Class
      package myClassSerialization;
      public class MathOperation {
           private int x;
           private int y;
           public MathOperation() {
           public void setX(int x) {
                this.x = x;
           public int getX() {
                return x;
           public void setY(int y) {
                this.y = y;
           public int getY() {
                return y;
      SerializeMathOperationClass Class
      package myClassSerialization;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      public class SerializeMathOperationClass {
           private MathOperation mathOperation = null;
           public void initializeData(int x, int y) {
                mathOperation = new MathOperation();
           public void serializeTheMathOperation() throws IOException {
                System.out.println("Creating File/Object output stream...");
              FileOutputStream fileOut = new FileOutputStream("MathOperation.ser");
              ObjectOutputStream out = new ObjectOutputStream(fileOut);
           public void deserializeTheMathOperation() throws IOException, ClassNotFoundException {
                MathOperation localMath = null;
                System.out.println("Creating File/Object input stream...");
              FileInputStream fileIn = new FileInputStream("MathOperation.ser");
              ObjectInputStream in = new ObjectInputStream(fileIn);
              localMath = (MathOperation) in.readObject();
              System.out.println("X = " + localMath.getX() + "\tY = " + localMath.getY());
           private void writeObject(ObjectOutputStream oOutS) throws IOException {
                System.out.println("Inside overriding writeObject");
           private void readObject(ObjectInputStream oInS) throws IOException, ClassNotFoundException {
                System.out.println("Inside overriding readObject");
           public static void main(String[] args) throws IOException, ClassNotFoundException {
                SerializeMathOperationClass serializeDemo = new SerializeMathOperationClass();
      Here I m trying to serialize the mathOperation field of SerializeMathOperationClass but I am getting the below error.
      Creating File/Object output stream...
      Exception in thread "main" java.io.NotSerializableException: myClassSerialization.MathOperation
           at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
           at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
           at myClassSerialization.SerializeMathOperationClass.serializeTheMathOperation(SerializeMathOperationClass.java:27)
           at myClassSerialization.SerializeMathOperationClass.main(SerializeMathOperationClass.java:66)
        1 2 Previous Next