5 Replies Latest reply: Feb 17, 2008 6:00 PM by EJP RSS


      Hi guys, just thought of something interesting. If I had these two classes:
      class A implements Serializable{
          int a1;
          int a2;
          public void method(){
      class B implements Serializable{
          int a1;
          int a2;
      and serialized an instance of A, and then tried to deserialize it as an instance of B, would it work?

      According to what I read, when serializing, java only stores the fields(the state of the instance) but no methods. so method() in class A wouldn't matter at all in serialization?

      When deserializing, would java look at the field names to match them or how?

      just thought this might be interesting.



      Edited by: J.T on Feb 16, 2008 5:31 AM

      Edited by: J.T on Feb 16, 2008 5:32 AM

      Edited by: J.T on Feb 16, 2008 5:34 AM
        • 1. Re: Serialization
          From what i have read by serialization, when you serialize a field it stores a class ID with the rest of the data, to avoid "errors" like this. And if the ID doesn't match properly when de-serializing, then you get an error.

          I think the class id is auto-generated if you do not choose a specific ID. I can't remember the field name you need though.

          I just looked it up in the API, and if you wanted the situation you described to work, you would have to manually set the serialVersionUID field (type long) in the object before it is serialized. and make it match the second class. (See API extract below)

          +The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

          ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;*

          If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.+
          • 2. Re: Serialization
            The class name is part of the serialized data and therefore
            serialization of class A will never be deserialized into class B.
            The serialVersionUID is not identifying the class (it's not a substitute
            for its name), it is there to distinguish between different versions of the same class. If you serialize version X and deserialize version Y,
            this would work. Fields that do not exist in both X and Y will get
            special treatment, as documented.
            • 3. Re: Serialization
              To understand it you need to know what is a Java Serial Version ID.
              Say you create a �Car� class, instantiate it, and write it out to an object
              stream. The flattened car object sits in the file system for some time. Meanwhile, if the �Car� class is modified by adding a new field. Later on, when you try to read (i.e. deserialize) the flattened �Car� object, you get the java.io.InvalidClassException � because all serializable classes are automatically given a unique identifier. This exception is thrown when the identifier of the class is not equal to the identifier of the flattened object. If you really think about it, the exception is thrown because of the addition of the new field. You can avoid this exception being
              thrown by controlling the versioning yourself by declaring an explicit serialVersionUID. There is also a small Java - Fundamentals
              performance benefit in explicitly declaring your serialVersionUID (because does not have to be calculated). So, it is best practice to add your own serialVersionUID to your Serializable classes as soon as you create them as shown below:
              public class Car {
              static final long serialVersionUID = 1L; //assign a long value
              • 4. Re: Serialization
                you can run the serialver tool to generate a serial ID.

                personally, i don't see much value in all this.

                • 5. Re: Serialization
                  ... and the serialVersioUID has nothing to do with the original question. If the OP had bothered to try it he would have got a ClassCastException.