Return to site

☕JAVA: How does Java use the serialVersionUID to identify Serializable classes?

· java

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