Having demonstrated how an object can be serialized from outside, the next step consists of showing how to create objects that are capable of serializing themselves. Sounds a little bit confusing? It isn't. Study the following class based on the previous example, which has the ability to serialize itself: class DataSaver{ As shown above, now the “DataSaver” class has an additional method, aside from the ones you saw in the previous example. In this case, the “saveSerialized()” method takes up the object itself, then serializes it and finally saves it to the specified text file. Quite interesting, right? An example that covers how to use this self-serializing feature is illustrated by the snippet below: // instantiate 'datasaver' object With reference to the capacity offered by the “saveSerialized()” method, all the objects that have the capability to serialize themselves are commonly called self-serializing objects, and generally are used as base objects for deriving other objects that persist during the execution of an application. But in fact, I’m getting ahead of myself. Read the next section of the article, where I’ll explain how to create objects that are capable of performing a serialization/unserialization sequence on themselves.
blog comments powered by Disqus |