0

I am trying to test the custom serialization but readObject() method is not being called.Although I am able to call the writeObject method. Can we overload the readObject method().

    public class Test {

        String pwd1 ;

        public static void main(String arg[]) throws IOException, ClassNotFoundException{

            FileInputStream fis = new FileInputStream("//vhub.lmera.ericsson.se//home//xsinkus//WDP//Desktop2k8R2//COMP_DOCS//Test//kusha1.ser");
            @SuppressWarnings("resource")
            ObjectInputStream ois = new ObjectInputStream(fis);
            System.out.println("I am being called ");
            SerilizationTest si = (SerilizationTest)ois.readObject();

            System.out.println("After called ");
            System.out.println(si.name);
            System.out.println(si.pwd);
            //si.readObject();
        }

          private void readObject(ObjectInputStream ois)
                    throws IOException, ClassNotFoundException {
              ois.defaultReadObject();
                     pwd1  = (String) ois.readObject();
                     System.out.println("I am in ");
                  }
    }
Jens
  • 67,715
  • 15
  • 98
  • 113
  • is `Test` supposed to be serializable? – Salem Jan 31 '17 at 12:30
  • 3
    You notice that you call `readObject()` on an instance of `ObjectInputStream` and not on an instance of `Test`? – Thomas Jan 31 '17 at 12:31
  • @Thomas `ObjectInputStream` uses the private `readObject` method of `Serializable` objects. [Another related question](http://stackoverflow.com/questions/12963445/serialization-readobject-writeobject-overides). – Salem Jan 31 '17 at 12:31
  • 2
    It doesn't look like you are actually calling the method. And you're not overloading anything, you've just created a method with the same name. – Steve Smith Jan 31 '17 at 12:31
  • 3
    @SteveSmith This comment is completely wrong - of course not! It can't be overloaded because it's private - [**it is still used**](http://stackoverflow.com/a/4119196/7366707) (if the object is `Serializable`.) – Salem Jan 31 '17 at 12:32
  • 1
    Yup! You can implement you own readObject/writeObject. Check these posts: [Uses of readObject/writeObject in Serialization](http://stackoverflow.com/questions/5911833/uses-of-readobject-writeobject-in-serialization) and [When to Override writeObject/readObject?](http://stackoverflow.com/questions/10770120/when-to-override-writeobject-readobject) – StackUseR Jan 31 '17 at 12:33
  • 2
    @1blustone you're right. Another question would be how those objects are being written, i.e. if the file actually contains data in the required format. – Thomas Jan 31 '17 at 12:34
  • 2
    Another question: why are you calling `defaultReadObject()` as well as `readObject()` right afterwards? As per JavaDoc `defaultReadObject()` does "read the non-static and non-transient fields of the current class from this stream", i.e. it should read `pwd1` and thus `readObject()` should run into problems. Do you get any exceptions? – Thomas Jan 31 '17 at 12:40

1 Answers1

1

Your readObject method needs to reside inside the class that implements Serializable. This way, only that class reads objects in this user defined manner.

Basically, it's not calling your method because it has no idea it exists.

CraigR8806
  • 1,584
  • 13
  • 21