254

When I used Eclipse it had a nice feature to generate serial version UID.

But what to do in IntelliJ?

How to choose or generate identical serial version UID in IntelliJ?

And what to do when you modify old class?

If you haven't specify the id, it is generated at runtime...

3
  • 1
    Duplicate of stackoverflow.com/questions/12912287/… Commented Jul 4, 2014 at 11:57
  • @JFIB The class constant serialVersionUID is generated by the compiler when not specified. Specifying has the advantage (and danger) of keeping the id identical for changed class versions. That is an file with an object of the old version will be read (deserialized) with the new version or vice versa. Commented Jul 4, 2014 at 12:01
  • 1
    @JFB What is a serialVersionUID and why should I use it? Commented Jul 4, 2014 at 12:02

4 Answers 4

519

Without any plugins:

You just need to enable highlight: (Idea v.2016, 2017 and 2018, previous versions may have same or similar settings)

File -> Settings -> Editor -> Inspections -> Java -> Serialization issues -> Serializable class without 'serialVersionUID' - set flag and click 'OK'. (For Macs, Settings is under IntelliJ IDEA -> Preferences...)

For Idea v. 2022.1 (Community and Ultimate) it's on:

File -> Settings -> Editor -> Inspections -> JVM Languages -> Serializable class without 'serialVersionUID' - set flag and click 'OK'

Now, if your class implements Serializable, you will see highlight and alt+Enter on class name will ask you to generate private static final long serialVersionUID.

UPD: a faster way to find this setting - you might use hotkey Ctrl+Shift+A (find action), type Serializable class without 'serialVersionUID' - the first is the one.

Sign up to request clarification or add additional context in comments.

5 Comments

@SalilJunior Not really. Built-In ability to generate a serialVersionUID is tied to inspection warnings. You're stuck when you do not want to enable the inspection warning but generate serialVersionUID in some cases.
This solution doesn't work for me, I can only see the options: remove Parcelable Implementation, replace parcelable implementation, create test, create subclass and make package-private. The class I'm using implements Parcelable and Serializable at the same time. Any idea?
Have you checked needed flag is set in settings above? Tested in both v16 and v17 idea, works fine. Also check additional settings for such inspection after click on it in settings window (to the right).
Thank you, but why is it not on by default?
Modern versions of IntelliJ have this bug/feature that you can't add a serialVersionUID to a static class. You have to temporarily make it non-static first to get the UID-code. Since e.g. the Collections-class uses serialVersionUIDs in static classes, I assume you should be able to have them even in these kinds of classes, so you could then just switch the class back to being static again.
291

Easiest method: Alt+Enter on

private static final long serialVersionUID = ;

IntelliJ will underline the space after the =. put your cursor on it and hit alt+Enter (Option+Enter on Mac). You'll get a popover that says "Randomly Change serialVersionUID Initializer". Just hit enter, and it'll populate that space with a random long.

7 Comments

Thanks worked for me in IntelliJ IDEA Community Edition version 2018.3.3
It is also advised in java docs to use private modifier where possible. docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/…
it works make sure you add semicolon at the end before (alt+Enter) for eg: private static final long serialVersionUID =(alt+enter) ;
The main problem which I see here is that you have to type all that modifiers by your own - private static final...
serialVersionUID should be a hashed value generated from the specification instead of a random value. I think the top voted answer from Serhii Maksymchuk is a better answer than this one as the serialVersionUID using his way seems generated from the signature of the Object.
|
36

Install GenerateSerialVersionUID plugin

5 Comments

Why install a plugin when this can easily be done as shown by @Serhii below.
@shaz I prefer when that inspection is disabled, but for some classes getting explicit serialVersionUID is required. I don't want distracting inspection on each class while I need serialVersionUID only for one.
I think the plugin helps with generating values for subclasses where the parent class is also serializable and contains a serialVersionUID. This takes care of the case when you have updated the child class and need to generate a new serialVersionUID for this class without changing the serialVersionUID of the parent class (in cases where no change was made to the parent class).
While this may answer the question, it is better to provide the actual information here and not just a link. Link-only answers are not considered good answers and will probably be deleted.
@EdCottrell There is no other information to be provided. Either use the plugin or not.
3

IntelliJ IDEA Plugins / GenerateSerialVersionUID https://plugins.jetbrains.com/plugin/?idea&id=185

very nice, very easy to install. you can install that from plugins menu, select install from disk, select the jar file you unpacked in the lib folder. restart, control + ins, and it pops up to generate serial UID from menu. love it. :-)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.