Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we use serialVersionUID during serialization [duplicate]

In my interview i got one question how to achive Serialization and I gave answer as

public class SerializationSample {
    public static void main(String args[]) {
        ...........
        FileOutputStream fos = new FileOutputStream(outFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(serializableObject);
        ............
}
class SerializationBox implements Serializable {
    ............
    ............
}

but the interviewer told, this will serialize whole object and he asked 2 questions

  1. How you will write a code if i want to serialize single object?
  2. Why we use long_integer id during serialization?

Please any one can help me to finding answer for this question

like image 714
Som Avatar asked Jun 30 '26 15:06

Som


1 Answers

Let me quote an essence from similar and already answered question

The serialVersionUID represents your class version, and you should increment it if the current version of your class is not backwards compatible with its previous version.

and

automatically-generated UID is generated based on a class name, implemented interfaces, and all public and protected members. Changing any of these in any way will change the serialVersionUID. So you don't need to mess with them only if you are certain that no more than one version of the class will ever be serialized (either across processes or retrieved from storage at a later time).

I recommend you to visit that link and, as always, search the web.

Hope that helps.

like image 139
Eel Lee Avatar answered Jul 03 '26 05:07

Eel Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!