Object persistence is where an object "persists" over space and/or time. An object that retains its state when it is saved to disk, and loaded from another location, or at a future time, is an object that exhibits the property of persistence.
For a computer gaming system, a gaming world can retain its state through a 'savegame'. We can save the game, and load it at a future point in time. The characters, locations, and physical objects remain. Most older computer games used a special file structure, that required special code to store and retrieve a gaming world. Using object persistence, we can write a game to a file, or read it from a file, with a few lines of code - no matter how large or complex our gaming world is!
The answer is simple - ease of use. Why design a special savegame format, write large amounts of code to save and restore games, and then have to modify that code everytime we build on the gaming system?
Object-orientated programming makes it easier for the programmer to write quality code that is easily maintainable. We break our gaming system up into objects, because this makes code easier to understand. Taking this train of thought one step further, since objects in Java can be made persistent very easily, it only makes sense to take advantage of this property.
In this tutorial, we'll cover how to make objects persistent, and how to restore their state from a local file. You'll need a JDK1.1 interpreter, as persistence isn't properly supported in JDK1.02. The demonstration runs as a standalone application, and not an applet, as it is important to show how to read & write files (an ability most applets do not have when running under a browser). Furthermore, I'll show you how to format text so that it automatically wraps around onto the next line - a feature missing from the applet example in the last tutorial.