FAQ for HW2

Last modified: August 7, 2002 1:45:58 AM PDT

  • How do I implement the Serializable interface?
  • What does the keyword transient mean and how is it used ...
  • Why do I keep getting the following runtime exception: "java.io.NotSerializableException: ...

    Back to the CS193J Homepage




    Q:

    How do I implement the Serializable interface?

    A:

    For this assignment, you probably will not need to define any methods in order to implement the Serializable interface. If fact, there are no public methods in this interface at all. Just include the "implements Serializable" line in your class definition. To serialize your collection of Shape objects, you simply need to iterate through the collection and call writeObject on each Shape. Alternatively, you could write the entire collection to disk as an object array using the toArray method. When loading a file, you will read these serialized objects back in and cast them as Shape objects to reconstruct your collection. If you wrote an object array to disk, then you will cast this as a Shape array when reading it back in. See the question and answer below regarding use of the keyword transient when serializing objects.
    Back to top...


    Q:

    What does the keyword transient mean and how is it used in serialization?

    A:

    You can use the keyword transient to mark a field within an object as something that should not be serialized. For example, you probably have a Shape class that contains a DrawingCanvas object. When you write a Shape object to disk, you don't want to write the DrawingCanvas to disk along with it because all of the Shape objects contain the same DrawingCanvas object. So, you should mark your DrawingCanvas variable in your Shape class as transient. The DrawingCanvas will get skipped over in the serialization process. Then, when you're reconstructing Shape objects during the load process, remember to set their canvas variables appropriately, since they will be null (remember, the DrawingCanvas objects weren't serialized).
    Back to top...


    Q:

    Why do I keep getting the following runtime exception: "java.io.NotSerializableException: DrawingCanvas$1"?

    A:

    You should be marking the DrawingCanvas reference in the Shape class as transient. If you do that, the runtime exception above will not occur. Some of you, however, did not mark the DrawingCanvas reference as transient and still did not encounter any runtime exceptions. The difference is that some of you used an anonymous inner class in your DrawingCanvas class. DrawingCanvas$1 is the name of the first anonymous inner class defined in the DrawingCanvas.java file. If you have other anonymous inner classes in the file, they will be named DrawingCanvas$2, DrawingCanvas$3, and so on (the classes are named for uniqueness - each anonymous inner class gets compiled into its own class file). Now, in order for a class to implement the Serializable interface, all of its object fields must also implement the Serializable interface. Unless you just happen to be defining an anonymous inner class whose supertype is serializable, your anonymous inner class will not be serializable. In this assignment, those of you who used an anonymous inner class in DrawingCanvas.java were probably implementing the ChangeListener interface, which is not serializable. That's the source of the runtime exception you've been getting. To avoid this, you must either: a) Refrain from using anonymous inner classes from within any class that you plan on making serializable (unless the anonymous inner class is a subtype of a serializable type), or b) Use the transient keyword to skip over non-serializable object fields (e.g. mark the DrawingCanvas reference in the Shape class as transient).
    Back to top...