Java Serialization and references

Say I have a serialized class that is used to save the state of my game. This serialized text is stored in a text file. If I restart my computer, reinstall java, etc. If I try to deserialize that text, will it save everything that it is referenced? For the purpose of question, assume the class has multiple ArrayList's of entitys and map elements. Class -> Serialization - > text Text - > Deserialization - > Class

Upvotes: 0

Views: 119

Answers (3)

Kostya Regent
Kostya Regent

Reputation: 183

Are you about standart java serialization mechanism? Java serialization mechanism will serialize data to binary format, not text. Java guarantees serialization/deserialization between versions until you use standart java library. So yes in your case serialization will work good.

But, I don't sure that standart java serialization good for your purposes. Because:

  • It's fully unreadable format.
  • If you'll change language later you must fully reimplement saving format.

As alternative you can use some of xml serialization libraries for java. In this case, save file will have readable format (good for debugging).

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

As long as the serialized classes don't change its definition, there won't be any problem. You may even move these serialized files into another OS which deserializes to the same classes definition and it will work with no problem (unless you use libraries specific to an OS, thus breaking portability).

Upvotes: 1

user197015
user197015

Reputation:

The serialized file will retain it's state regardless of whether or not you reinstall Java or restart your machine. It would be pretty useless otherwise: the point of serialization is to capture state in a persistent form for archival or transport in a fashion that can be to recreate that state later.

So, assuming the serialization method you are using originally saves all the references you care about, then you'll always be able to restore that object and all its references from that serialized data. Unless, perhaps, you try in five years using a new version of Java that no longer supports that serialization format or something.

Upvotes: 0

Related Questions