I frequently use the Eclipse quick fix to generate a serial version UID for serializable classes.
Is there any way to do this by default?
It's one extra click, but with Eclipse selecting quick fixes is generally slowish, and I always generate serial version UID:s when I implement serializable.
File -> Settings -> Editor -> Inspections -> Java -> Serialization issues : Find serialization class without serialVersionUID and check it. Back to the editor, clicks on the class name, ALT + ENTER (Windows), it will prompts the Add serialVersionUID field option. A new serialVersionUID is auto-generated.
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.
If class does not contain a serialVersionUID field, its serialVersionUID will be automatically generated by the compiler. Different compilers, or different versions of the same compiler, will generate potentially different values.
This is perhaps not the answer you're looking for. Presumably, you use Eclipse to generate the same special value that Java would automatically calculate for your class if you didn't explicitly specify it, something like:
private static final long serialVersionUID = 4125965356358329466L;
But there's no good reason to rely on this same algorithm for newly authored classes. What matters is you specify a value, any value. So why not simply do the following?
private static final long serialVersionUID = 1L;
You could then put this code in Eclipse's new class template.
Not sure if you have got answer to this. But Eclipse does allow to create serialVersionUID
in one go for all classes implementing Serializable
. Although, it is not exactly same what you want. However, it will serve the purpose with fewer clicks.
Right-click Project -> Source -> Clean Up...
After clicking Finish, Eclipse will generate serialVersionUID
.
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