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?