问题
What happens if you take an old serializable object that never had serialVersionUID explicitly specified, and add serialVersionUID to that object? It seems to me that the next time the app was updated by endusers it would try to deserialize data from disc, find out that the serialVersionUID didn't match, overwrite the data with new data from the server/db/whatever and after that you're fine. Am I correct in this assumption? Are there further issues I should be wary of in doing this?
private class X implements serializable {...
private static final long serialVersionUID = 0L;
回答1:
- If you give it the same value that is shown by running the
serialver
tool on the class as it is now, nothing happens. - If you give it a different value, further deserializations of existing serial streams including that class will fail with an
InvalidClassException
.
It seems to me that the next time the app was updated by endusers it would try to deserialize data from disc
Correct.
find out that the serialVersionUID didn't match
Only if it really didn't match. If you follow the advice above, it will match.
overwrite the data with new data from the server/db/whatever
Incorrect. See above.
and after that you're fine.
No.
Am I correct in this assumption?
No.
来源:https://stackoverflow.com/questions/32069590/android-what-happens-if-i-add-serialversionuid-to-old-serializable-objects