Use serialVersionUID or suppress warnings?

I want to create a class that, for example, extends HttpServlet? My compiler warns me that my class must have serialVersionUID. If I know that this object will never be serialized, should I define it or add an annotation to suppress these warnings?

What would you do and why?

+62
java serialization serialversionuid
Sep 28 '08 at 20:51
source share
12 answers

I donโ€™t know the best Java practices, but it occurs to me that if you say serialization will never happen, you can add the writeObject method, which produces. Then suppress the warning, safe in the understanding that it cannot be applied to you.

Otherwise, someone may serialize your object in the future through the parent class and eventually get the default serialized form:

  • the form is incompatible between different versions of your code.
  • You suppressed a warning that this is so.

Adding an identifier sounds like a trap, because what you really want to do is not serialized. Expecting callers to not serialize your object, you expect them to โ€œknowโ€ when their HttpServlet belongs to your class. This is a violation of polymorphism on your head because it has a Serializable object that should not be serialized, and at least you can do this to make sure that careless users know about it.

+26
Sep 28 '08 at 21:12
source share

If you do not plan to serialize instances, add SuppressWarning.

The generated serial identifier can be a little dangerous. This assumes that you intentionally gave it a serial number and retained serialization and deserialization. It's easy to forget to update the serial number in the new version of your application, where your class is changed. Desicialization will fail if the class fields have been changed. Having SuppressWarning at least tells the reader of your code that you are not going to serialize this class.

+17
Sep 28 '08 at 20:58
source share

I don't want to be terrorized by Eclipse by adding clutter to my code!

I just configure Eclipse to not generate warnings in the absence of serialVersionUID.

+14
Sep 28 '08 at 21:35
source share

Thanks @ Steve Jessop for his answer to this. It was 5 lines of code ... hardly a hassle.

I added @SuppressWarnings("serial") just above the class in question.

I also added this method:

 private void writeObject(ObjectOutputStream oos) throws IOException { throw new IOException("This class is NOT serializable."); } 

Hope Steve meant :)

+7
May 14 '10 at 3:24 a.m.
source share

Even if you know that this object will be serialized, there is no need to generate serialVersionUID, because java will automatically generate it for you and automatically track changes, so your serialization will always work fine. You should only generate it if you know what you are doing (serial sequence compatibility, manual change tracking, etc.).

Therefore, I would say that suppressing a warning is the best and safest solution in most cases.

+6
Sep 28 '08 at 21:55
source share

Let Eclipse generate an identifier. Quick and easy. Warnings cannot be ignored. It will also save a lot of trouble if you ever get to the point where the object / should / be serialized.

+3
Sep 28 '08 at 20:52
source share

This warning makes me crazy, because every time you subclass a Swing class, you know that you are never going to serialize it, but there is a silly warning. But yes, I let Eclipse generate one.

+3
Sep 28 '08 at 21:29
source share

It is good to generate SVUIDs for each class that implements serializable. The reason is simple. You will never know when it will be serialized by you or by any third party. Many services can be configured to serialize servlets. For each IDE, there is a plugin that generates one or just use a template and sets svuid = 1L.

+3
Sep 29 '08 at 14:02
source share

If you do not specify serialVersionUID, java will generate one for the class at compile time (it changes with each compilation).

When deserializing objects, the serialVersionUID of the deserialized object is compared to a class of the jvm class. If they are different, they are considered incompatible and an exception is thrown. This can happen, for example, after updating your program and deserializing old classes.

I always use 1L for serialversionUID. This will not hurt (compared to the one created by default), and it remains possible to unlock compatibility later by increasing the identifier.

+2
Sep 29 '08 at 9:59
source share
+2
Mar 17 '09 at 17:23
source share

It depends.

If you use different compilers to compile your source code several times, your compiled code may have different serializationIds parameters that violate serialization. Then you need to stick with constant serializationId explicitly in your code. It must be static, finite, and for each class (not inherited).

However, if you always compile your code with a specific compiler and always deploy your code in one shot to all your virtual machines, you probably need strict version checking and you want to make sure that at any time there is only one version of your code in this case you should just suppress the warning. Thus, if the virtual machine does not successfully deploy and the old version of your code is running, you probably expect exceptions during serialization, rather than fancy deserialized objects. This happens in my case, we had a very large cluster, and we need strict version checking to find out about any deployment problem.

In any case, perhaps you should avoid serialization when possible, since default serialization is very slow compared to protocol buffers or saving and does not support cross-language interactions.

+2
Jun 08 2018-11-11T00:
source share

If you know that your applications never serialize something, suppress the warning throughout the program. This can be done using javac command line arguments:

javac -Xlint -Xlint:-serial *******

This way you will see all warnings except "serial". IDE files and build tools like Maven / SBT / Gradle work fine with this.

0
Dec 15 '15 at 11:01
source share



All Articles