Return to site

โ˜•JAVA: How does Java use the serialVersionUID to identify Serializable classes?

January 6, 2024

The serialVersionUID is a unique identifier that is used to serialize and deserialize an object of a Serializable class in Java.

It helps to verify ๐Ÿ•ต๏ธ that the sender and receiver of a serialized object have loaded classes that are compatible ๐Ÿค with respect to serialization.

If the serialVersionUID of the sender and receiver classes do not match, an InvalidClassException โŒ will be thrown.

The serialVersionUID can be either declared explicitly by the programmer ๐Ÿง‘โ€๐Ÿ’ป, or generated automatically by the Java Virtual Machine (JVM) โ˜•๐Ÿค– based on the class definition.

โ˜๏ธ However, it is recommended to declare the serialVersionUID explicitly, as the default value may vary depending on the JVM platform and version.

To declare the serialVersionUID explicitly on IntelliJ, you can use the following method:

๐€๐ฅ๐ญ+๐„๐ง๐ญ๐ž๐ซ ๐จ๐ง

๐ฉ๐ซ๐ข๐ฏ๐š๐ญ๐ž ๐ฌ๐ญ๐š๐ญ๐ข๐œ ๐Ÿ๐ข๐ง๐š๐ฅ ๐ฅ๐จ๐ง๐  ๐ฌ๐ž๐ซ๐ข๐š๐ฅ๐•๐ž๐ซ๐ฌ๐ข๐จ๐ง๐”๐ˆ๐ƒ = ;

IntelliJ will

* underline the space after the =.

* put your cursor on it and

* hit alt+Enter (Option+Enter on Mac).

* You'll get a popover that says "Randomly Change serialVersionUID Initializer".

* Just hit enter, and

* it'll populate that space with a random long.

#java #serialization #uuid #programming #intellij

Stackoverflow answer๐Ÿ‘‰ https://stackoverflow.com/questions/24573643/how-to-generate-serial-version-uid-in-intellij#53824823

Udemy๐Ÿ‘‰ https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A