I create a class which extends Exception
class, I got this warning on Eclipse
The serializable class PhoneAlreadyExists does not declare a static final serialVersionUID field of type long
how to remove it please ?
public class PhoneAlreadyExists extends Exception {
public PhoneAlreadyExists() {
// TODO Auto-generated constructor stub
super();
}
PhoneAlreadyExists(String message) {
super(message);
}
}
If serialVersionUID is not provided in a Serializable class, the JVM will generate one automatically. However, it is good practice to provide the serialVersionUID value and update it after changes to the class so that we can have control over the serialization/deserialization process.
However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during ...
SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.
To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.
To change the behavior in eclipse globally: Go to Preferences->Java->Compiler->Errors/Warnings->Potential Programming Problems. There's an entry for this specific problem. You can change it for a specific project too.
That answers your question, although - I'd recommend to leave it at warning level and add the missing fields. Or add the SuppressWarnings annotation to those serializable classes that really do not need the field because they'll never be serialized.
Add an annotation @SuppressWarnings("serial")
on your class, to ignore that warning
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With