Whenever you write a Java class in Eclipse which implements java.io.Serializable
interface, you’ll get this warning:
The serializable class XXXX does not declare a static final serialVersionUID field of type long
Why do we need serialVersionUID?
Whenever we implement java.io.Serializable
interface, we explicitly tell JVM that this class can be serialized. Meaning, we might convert object of this class into byte streams and write it into a file or send it to another JVM through network.
Now when Java objects use serialization to save state in files, or as blobs in databases, the potential arises that the version of a class reading the data is different than the version that wrote the data.
Thus the serialVersionUID provides a versioning mechanism to Java class so that the same class format is used in reading (de-serialization) and writing (serialization) of objects.
But in most of the cases, you will not need this field. You will not have more than one versions of class file which creates issues in deserialization. And that’s why most of the time I skip adding a serialVersionUID version number.
How to ignore serialVersionUID warning
In Eclipse, you can permanently mute the serialVersionUID warning by:
1. To ignore warning for specific eclipse project:
- Right click on Project, select Properties from context menu (Shortcut: Alt+Enter)
- Go to Java Compiler > Errors/Warnings
- Under Potential programming problems section, change Serializable class without serialVersionUID: to Ignore
Or you may want to ignore this warning at workspace level.
2. To ignore warning at workspace level:
- Go to menu Windows > Preferences
- Navigate to Java > Compiler > Errors/Warnings
- Under Potential programming problems section, change Serializable class without serialVersionUID: to Ignore
Wonderful blog Viral. Keep it up. By d way, a small typo in the above post – Reading[serialization] and writing [de-ser] ..it should be reverse.. reading[de-ser] and writing[serialization].
ops.. well caught.. I am currently on vacation.. I will fix this as soon as I get access to my computer.. thanks :)
Good Trick.
Many thanks for this. :)
Thank you! I’m learning Java and this Eclipse warning gave me a minor headache.
A lot of thanks!