• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Changing a serialized class

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class Level which implements serialized. Let's say it looks like this


While running the program I set some values to the width and height and save it to a .bin file. When I then close the program, add for example a and then open the program again and load the level the name will be null. How do I make it so that the new field takes a default value from the constructor? Or do I need to write a script to give the level a default value for the new field every time I add a new field?
 
Saloon Keeper
Posts: 9712
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not following you. Your Level class does not have a field called 'name'.
 
Luc Drenth
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm not following you. Your Level class does not have a field called 'name'.



I mean if I add the "name" field after saving the old Level class without the name field.
 
Saloon Keeper
Posts: 14488
325
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can add the following method to your class:


Java's serialization mechanism uses a lot of magic and in general causes more pain than it's worth. If your object graph doesn't consist of a lot of binary data, you might want to consider using JAX-B instead and serializing your objects to XML.
 
Saloon Keeper
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's more, serialization only works well between the same JVM versions. If both ends of the communication use different versions, all bets are off. Like Stephan says, use XML or JSON in that case.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic