229

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

Helder Pereira
  • 5,522
  • 2
  • 35
  • 52
catch23
  • 17,519
  • 42
  • 144
  • 217
  • 1
    Duplicate of http://stackoverflow.com/questions/12912287/intellij-idea-generating-serialversionuid – Bruno Volpato Jul 04 '14 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. – Joop Eggen Jul 04 '14 at 12:01
  • 1
    @JFB [What is a serialVersionUID and why should I use it?](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it) – catch23 Jul 04 '14 at 12:02

4 Answers4

487

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.

Serhii Maksymchuk
  • 5,124
  • 2
  • 14
  • 20
  • 2
    @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. – Fabian Barney Aug 09 '17 at 09:55
  • 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? – gonver Oct 16 '17 at 12:54
  • 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). – Serhii Maksymchuk Oct 17 '17 at 22:11
  • Thank you, but why is it not on by default? – Marco Sulla Feb 28 '23 at 14:10
236

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.

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
  • Thanks worked for me in IntelliJ IDEA Community Edition version 2018.3.3 – Tarun Jan 18 '19 at 12:35
  • It is also advised in java docs to use private modifier where possible. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/Serializable.html – Ilya Lozinsky May 15 '19 at 06:20
  • 1
    it works make sure you add semicolon at the end before (alt+Enter) for eg: private static final long serialVersionUID =(alt+enter) ; – Nisarg Bhagavantanavar Aug 02 '21 at 07:37
  • The main problem which I see here is that you have to type all that modifiers by your own - `private static final...` – catch23 Sep 17 '21 at 16:40
  • `serialVersionUID` should be a hashed value generated from the [specification](https://docs.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100) 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. – ikhvjs Oct 26 '22 at 11:15
36

Install GenerateSerialVersionUID plugin

Dark Star1
  • 6,986
  • 16
  • 73
  • 121
Meo
  • 12,020
  • 7
  • 45
  • 52
  • 4
    Why install a plugin when this can easily be done as shown by @Serhii below. – PineCone Apr 25 '17 at 08:34
  • 2
    @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. – Timofey May 02 '17 at 12:42
  • 1
    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). – kapad Apr 02 '18 at 13:15
  • 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](http://stackoverflow.com/help/deleted-answers). – elixenide Jul 21 '18 at 06:25
  • 4
    @EdCottrell There is no other information to be provided. Either use the plugin or not. – Meo Jul 21 '18 at 07:17
2

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