Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing a method from a Serializable class  RSS feed

 
Dalia Sultana
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does removing a method break serialization? I ran into a class that implements Serializable. It doesn't declare a version uid. It seems like the application is throwing an InvalidClassException after removing a method, although the method is unused. I am wondering if this is the reason why I am getting the error or something else might have triggered it.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removing a method definitely would change the generated serialVersionUID, yes.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the non-transient instance fields have not changed, neither in name, type or order, you could get it to work. You need to get the serial version UID of the class before removing the method, and then declare it inside your class:

If you declare this field, it will be used. If you don't then a generated value will be used, based on all instance members of the class. You can use the serialver.exe tool to get that generated value.

You must beware though; if you do change the non-transient instance fields but keep the same serialVersionUID, things can go horribly wrong when deserializing an older version.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!