1

I am having trouble understanding this comment from the Java serialization documentation:

Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.

Maybe I am unable to understand the obvious, however, I haven't figured why would I need to do this?

Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
Ashley
  • 629
  • 3
  • 6
  • 16

2 Answers2

2

I think that the answer to your question is to read up on serialVersionUID. In particular, you often want to specify this on your classes to be able to deal with the fact that classes change over time.

The comment that you are referring to is saying that you cannot set a serialVersionUID on arrays. If this was true, and the serialVersionUID always needed to match, then your array would have serious issues, and would be very fragile with respect to serialization. To counter this, the language designers decided that array (de)serialization would ignore the serialVersionUID, thus side-stepping the problem that it cannot be defined.

Community
  • 1
  • 1
Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
  • thanks. So, you are saying that classes that comprise of array variables, cant have serialVersionUID? I have used int[] arrays and object[] along with serialVersionUID in different classes for object based communication over sockets and arrays have always come up just fine across the wire. May be I am unable to understand your point clearly. Is it possible to paste a sample code? I would greatly appreciate it. – Ashley Oct 07 '13 at 20:19
  • @Ashley, not quite. A class that contains an array can have a serialVersionUID, however the array itself cannot. – Paul Wagland Oct 07 '13 at 20:31
  • Thanks again. So, its actually the array itself. I think the term "Array classes" is a bit confusing. Thanks for clearing this up. – Ashley Oct 07 '13 at 20:38
  • 'Your' array class (*what* array class?) would have *what* serious issues, and would be very fragile *how?* – user207421 Aug 13 '15 at 06:03
  • @EJP 'Your array class' == the array class that you created. It would be fragile WRT serialization since you wouldn't be able to guarantee that they work. – Paul Wagland Aug 24 '15 at 20:30
0

I haven't figured why I would need to do his.

You don't. You can't. There is no syntax for defining array classes, let alone their serialVersionUID members.

That's also why checking it would be completely pointless. There is nothing about the class that could possibly change.

user207421
  • 305,947
  • 44
  • 307
  • 483