Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove the warning The serializable class ClASSNAME does not declare a static final serialVersionUID field of type long [duplicate]

Tags:

java

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);
    }

}
like image 666
user2059935 Avatar asked Feb 23 '13 21:02

user2059935


People also ask

What will happen if I will not declare serialVersionUID in a class which implements Serializable?

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.

Is Serializable consider declaring a serialVersionUID?

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 ...

What is the role of serialVersionUID in serialization process?

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.

How do you stop serialization in Java?

To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.


2 Answers

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.

like image 163
Andreas Dolk Avatar answered Oct 23 '22 21:10

Andreas Dolk


Add an annotation @SuppressWarnings("serial") on your class, to ignore that warning

like image 27
Arsen Alexanyan Avatar answered Oct 23 '22 20:10

Arsen Alexanyan