Tuesday, December 03, 2013

Eclipse “Not Declare Static Final SerialVersionUID” Warning


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 (serialization) and writing (deserialization) of objects.

Ignore serialVersionUID warning
In Eclipse, you can permanently mute the serialVersionUID warning by:
1. To ignore warning for specific eclipse project:
  1. Right click on Project, select Properties from context menu (Shortcut: Alt+Enter)
  2. Go to Java Compiler > Errors/Warnings
  3. Under Potential programming problems section, change Serializable class without serialVersionUID: to Ignore




No comments:

Post a Comment

Distributed Transactions

What is a distributed transaction?  Transactions that span over multiple physical systems or computers over the network, are simply termed D...