0

I'm wondering how serialized object could be safely used through different flavour of the same program. Think two classes A and B like these:

public class A implements Serializable
{
    private String myText = null;

    public A()
    {
        myText = B.text;
        B.myMethod();
    }  
}

In a different file:

public class B
{
    /* EDITED */
    private static A myA = null;
    public static String text = "B1_text";

    public static void saveA()
    {
        myA = new A();
        /* blablabla */
        objectOutputStream.writeObject(myA);
        /* blablabla */
    }

    public static void loadA()
    {
        /* blablabla */
        myA = objectInputStream.readObject();
        /* blablabla */
    }

    public static void myMethod()
    {
        /* some stuff */
    }

    public static void not_A_related_method()
    {
        /* some stuff */
    }
}

Now i open my program, call

B.saveA();

save A to a file and then close the program. If I later load A from the file, calling

B.loadA();

nothing bad would happen.

But what if I change the class B (after saving the class A to a file from the untouched class B) to something different like:

public class B
{
    /* EDITED */
    private static A myA = null;
    public static String text = "B2_COMPLETELY_DIFFERENT_TEXT";

    public static void saveA()
    {
        myA = new A();
        /* blablabla */
        objectOutputStream.writeObject(myA);
        /* blablabla */
    }

    public static void loadA()
    {
        /* blablabla */
        myA = objectInputStream.readObject();
        /* blablabla */
    }

    public static void myMethod()
    {
        /* some NEW stuff */
    }

    public static void not_A_related_method()
    {
        /* some NEW stuff */
    }

    public static void ANOTHER_not_A_related_method()
    {
        /* some stuff */
    }
}

And then i call

B.loadA(); //(loading a previously saved file)

What would happen really?

I experienced that everything goes well, but far can one go changing statically referred methods and fields from Serialized object?

iMineLink
  • 368
  • 1
  • 3
  • 14
  • I fail to see your point. The class of the object being serialized does not look like as if it was being changed at all, so why should `loadA()` fail? – SJuan76 Aug 19 '13 at 13:37
  • And of course Tala's post is right, your code does not compile. – SJuan76 Aug 19 '13 at 13:38
  • I edited the code and now myA is declared static – iMineLink Aug 19 '13 at 14:09
  • @SJuan76 I though that something of the static referred methods or fields would be included in the serialization. As I (poorly) know, the serialization algorithm serializes the referred objects of the serialized class instance as well as the class instance itself; Dunno what happened to static things. – iMineLink Aug 19 '13 at 14:20

2 Answers2

0

But what if I change the class B (after saving the class A to a file from the untouched class B) to something different like:

Deserialization doesn't care which particular method serialized the object. Even if this method was changed. The main thing is that you shouldn't make class A incompatible so that it can be deserialized.

If you want to statically [de]serialize object you might consider using Singleton pattern

You can serialize an object on a desktop and then deserialize it in Android. All you need to care about is compatibility of classes.

They can be different (for example have different methods) but ensure that they have same SerialVersionUID. You can find a good explanation here of what it is.

Community
  • 1
  • 1
Tala
  • 8,888
  • 5
  • 34
  • 38
  • That's true, I made a mistake, myA is to intend static (I'll now edit). I wanted to load the same object from a Java Desktop program and from an Android application, that's what I asked the question for. I'll take also a look at the Singleton pattern, thanks. – iMineLink Aug 19 '13 at 14:04
  • I see, so added the explanation for your case. – Tala Aug 19 '13 at 14:12
0

Static fields aren't serialized, so both versions would work fine. Old serialized files can be read in new versions of the program, new files could be read in old versions of the program.

The static string is not stored in the serialized file. Any logic relying on that static string to be some value would still work.

William Morrison
  • 10,953
  • 2
  • 31
  • 48