0

Am trying to add my class in cache. If i do that it throws error like

Caused By: java.io.NotSerializableException: com.test.package.PropertyGroup
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)

So i came to know that my object should implement Serializable interface for my class. And i have implemented Serializable interface. But it shows warning that my class should have serial version ID with two options default serial version ID and generated serial version ID . What is the difference between these two i.e serialVersionUID =1L and some random generated value serialVersionUID = 8243252575074067947L;

Arun
  • 609
  • 2
  • 12
  • 33
  • Dupicate of: http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it – rghome May 29 '15 at 07:52
  • I don't think that it is a duplicate, as this question explicitly addresses the generation options in the IDE. – Steven Pessall May 29 '15 at 07:59

2 Answers2

1

If you have existing instances of your class serialized somewhere and you need to deserialize them, use the generated version of the class before your current change. This is because the class loaded into your JVM needs to have the same serialVersionUID as the serialized instance. Since the serialized instance didn't have it set explicitly, it would be the automatically generated ID.

If you don't have any instances in the wild that need to be deserialized, then the default ID is nicer to read and easier to manually increment as needed.

Since it sounds like you've never serialized this class before (as you're just now marking it as Serializable), use the default 1L version.

Yosef Weiner
  • 5,432
  • 1
  • 24
  • 37
0

The generated value is not random. It is the serialVersionUID which the JRE would use implicitly if none is specified in a serializable class.

This is important if you need to maintain compatibility. In this case you would generate the serialVersionUID before making any changes to the class. Afterwards you can modify the class, e.g. by adding fields, without causing SerializationErrors for users of an old version of the class.

In your case this does not seem to be a problem, so you can just use the default option. But either option will work.

Steven Pessall
  • 983
  • 5
  • 15
  • So you mean if i use system generated iD and if i do changes to my class, i have to update my serial version id? – Arun May 29 '15 at 08:09
  • No, the intention of the serialVersionUID is that it remains the same as long as you are making "compatible" changes. Have a look at the question linked by rghome for more infos on the serialVersionUID. – Steven Pessall May 29 '15 at 08:14