title: Serializing objects in java
body: <p>Just did a brief reading to try and implement serialization into my code.</p>
  
  <p>Basically, I have a <code>Test</code> class that holds a bunch of <code>Questions</code>. I would either like to serialize the whole <code>Test</code> object at once if possible, or if not each <code>Question</code>. Right now I am trying to do each <code>Question</code>.</p>
  
  <pre><code>public abstract class Question implements Serializable {
      ...
  }
  
  public class Test {
  
  ...
  
  public void save() {
      try {
  
          System.out.print("File name will be saved.ser: ");
  
          FileOutputStream fileOut = new FileOutputStream("saved.ser");
  
          ObjectOutputStream out = new ObjectOutputStream(fileOut);
  
          for (Question question : questionList) {
              out.writeObject(question);
          }
  
          out.close();
          fileOut.close();
  
      } catch(IOException i) {
          i.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And then it gives this error:</p>
  
  <pre><code>java.io.NotSerializableException: java.util.Scanner
      at java.io.ObjectOutputStream.writeObject0(Unknown Source)
      at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
      at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
      at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
      at java.io.ObjectOutputStream.writeObject0(Unknown Source)
      at java.io.ObjectOutputStream.writeObject(Unknown Source)
      at main.Survey.save(Survey.java:129)
      at main.MainDriver.main(MainDriver.java:59)
  </code></pre>
  
  <p>Is the problem that not every object in <code>Question</code> implements <code>Serializable</code>? Does that need to be done as well? That seems quite tedious, but I'll do it if necessary. Do all of my classes that are used by <code>Question</code>, including it's subclasses, need to be given that interface?</p>
  
  <p>Also, would I be better off Serializing the whole <code>Test</code> object in my main method?</p>