0

I just want to know why we take only long type serialVersionUID variable in java

`public static final long serialVersionUID = 1L;`

I mean i know how it works + what is serialization etc.

But i am just curious to know that why we only take long primitive type ? why not int or any other type?

like this

 public static final int serialVersionUID = 1;

Is java specification tell us to do that?

GhitaB
  • 3,275
  • 3
  • 33
  • 62
Gurinder
  • 943
  • 2
  • 9
  • 19
  • pls read my question carefully :) – Gurinder Nov 02 '15 at 09:22
  • This question is a total duplicate IMO, but I will add here that by using `long` the Java specification makes collisions between objects that much less likely than using a smaller type such as `int`. – Tim Biegeleisen Nov 02 '15 at 09:22

1 Answers1

3

Quote from the docs of java.io.Serializable

A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long.

So yes the specification tells us to use long. Otherwise it won't be recognized as the serialVersionUID.

Chris
  • 7,675
  • 8
  • 51
  • 101
  • thanks for replying but any reason why long type ? As @Tim commented above bcoz of less collision between objects – Gurinder Nov 02 '15 at 09:28
  • 1
    I don't have a source for that but i think its just that long is "longer" and as such can have much more different value than int. I think they chose it to be sure to make the serialVersionUID as distinct as possible. SO with an int if you had more than (2^32)-1 diffrent revisions of your class you'd have a problem. – Chris Nov 02 '15 at 09:29