1

Here in my class I have provided the serialVersionUID explicitly, but while using 'serialver' command in command prompt for this same class the UID is generated by the system as well. enter image description here
Could anyone please throw some light over the understanding of use for explicit and implicit SerialVersion UID?

   class Emp implements Serializable {
 private static final long serialversionUID =
                             1293488886969693L;


       int age;


 public Emp(int age)
 {
     this.age = age;
 }

 }
Maha-Dev
  • 56
  • 6
  • 2
    Implicit UID may change from VM to VM, as it is created with information from the class. Thus 2 Classes in 2 different VMs which may be the same don't have the same UID, which breaks serialization – Lino Jun 01 '18 at 05:42
  • I understand the scenario elaborated but what I don't understand is the use or application of explicit ID as if the VM is generating its separated ID where will the explicit ID will be used or can be used for deserialization? Or anywhere in that context. – Maha-Dev Jun 01 '18 at 05:46
  • 2
    An explicitly defined serialVersionUID is *not* required. Some IDE's will issue a **warning** but not an error for it (unless you change the default settings and turn this warning into an error yourself) – Erwin Bolwidt Jun 01 '18 at 05:53
  • If it is not required then why is it a technical liberty or why is it allowed to do? ANd what does it technically imply when we do this? – Maha-Dev Jun 01 '18 at 05:58

1 Answers1

4

serialver is just a tool to compute such ID on a given class. Java defines an way to compute IDs from the class definition. Defining it in the class is just a way to choose that ID. Many IDEs automatically computes its value with the use of serialver, but you can edit the value by yourself and choose any one you want.

You may read What is a serialVersionUID and why should I use it?

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • I understand the scenario elaborated but what I don't understand is the use or application of explicit ID as if the VM is generating its separated ID where will the explicit ID will be used or can be used for deserialization? Or anywhere in that context. – – Maha-Dev Jun 01 '18 at 05:56
  • 1
    When you explicitly define that UID, it will be used, else, the implicit will be generated and used – Lino Jun 01 '18 at 06:05
  • @Maha-Dev you may have your own policy for generating IDs. So explicit ones is a way to enforce it. – Jean-Baptiste Yunès Jun 01 '18 at 06:47
  • But what is happening here is when I am serializing the objects of this (Emp) class and after serialization when modifying explicitly defined ID, then in my understanding, the deserialization should generate conflict between the states of class before and after serialization, i.e., dependent over the factor of "ID value" modified after serialization. But after the observation, I found that the ID value used while serialization and deserialization are implicit and not the explicit. And this raises the question for me as to where and when I should use explicit ID. – Maha-Dev Jun 01 '18 at 10:15
  • Without being able to observe what you are doing, hard to answer. The important thing is that when deserializing you need to use a class whose ID is the same as the one used for serializing. But that value is not important perse. – Jean-Baptiste Yunès Jun 01 '18 at 13:02
  • As for now, I would take the answer to be as that the Explicit Serial Version Id is not the only key player in serializing and deserializing. – Maha-Dev Jun 05 '18 at 02:40