Regarding serialization, serialVersionUID, and incompatible class
<p>I saw a few related questions and answers here, but they didn't seem to help. So I guess I need to post a separate question here:</p>

<p>I have a serializable class <code>MyDataClass</code>, which contains a few more serializable inner classes. I save it to a file by using <code>FileOutputStream</code> and <code>ObjectOutputStream</code>. When reading it back, I sometimes get an exception (maybe <code>ClassNotFoundException</code> or <code>InvalidClassException</code> but I'm not sure), and the exception's message goes like this:</p>

<pre><code>12-01 18:44:16.479: D/My App Name(534): ***** exception occurred 
com.xyz.myapp.lib.am; Incompatible class (SUID): com.xyz.myapp.lib.am: 
static final long serialVersionUID =3894560643019408205L; but expected 
com.xyz.myapp.lib.am: static final long serialVersionUID =-4215454881436014736L;
</code></pre>

<p><code>MyDataClass</code> is something like this:</p>

<pre><code>public static class MyDataClass implements Serializable {

    private static final long serialVersionUID = 0x98F22BF5;
    .....
    public boolean bNoTitle;
    public short syncOption;
    .....

    public class InnerClass implements Serializable {
        private static final long serialVersionUID = 0x99D32720;
        double premium;
        double interest;
    }
}
</code></pre>

<p>Then I used the following code to read it:</p>

<pre><code>MyDataClass oc;
FileInputStream fin = new FileInputStream(dataFile);
ObjectInputStream ois = new ObjectInputStream(fin);
oc = (MyDataClass)ois.readObject();
ois.close();
fin.close();
</code></pre>

<p>An exception is thrown by readObject, NOT EVERY TIME, but sometimes that I can't pinpoint so far. I read somewhere that the serialVersionUID that's actually written into the file is not what I assigned in the code. So when it's read back, the UID does not match, and thus the exception. Is that right? If so, how can I ensure that there is no exception and the object is read back as expected?</p>

<p>thank you.</p>

<p><strong>EDIT:</strong> It seems that when debugging from Eclipse, it was all fine. But after I made a build using <code>ant</code>, the problem would surface. But not every time after an <code>ant</code> build, but sometimes.</p>

<p><strong>EDIT:</strong> I saw in another post: </p>

<p><em>Are you reading from a file? In that case, it does not matter if you added the serialVersionUID now, it is different from the one stored in the file, and that creates the exception. A quick solution could be to set serialVersionUID to 4209360273818925922L, which seems to be the serialVersionUID that was automatically generated by java when you saved those object in that file at that time.</em></p>

<p>My file will be saved/read many times when the user uses the app. If Java assigns a different UID <strong>every time</strong> the file saves, then how can I wire-in a number in the code before hand? I need a truly static UID that doesn't change, even after an <code>ant</code> build.</p>
