title: writeObject(this) cannot write object, java
body: <p>I am using ObjectOutputStream to save object, but when I use .writeObject(this) to save it as a file, the material cannot be saved. The class I defined is already serializable.</p>
  
  <pre><code>public class LanguageModel implements Serializable {
  
  
  private static LanguageModel lm_;
  
  /* ******************************* */
  //word -&gt; count(w)
  public static Dictionary unigramDict = new Dictionary();
  //word_pair -&gt; count(wi,wi+1)
  public static Dictionary bigramDict = new Dictionary();
  
  private static int wordIdCounter = 0;
  /* ***************************** */
  
  
  // Do not call constructor directly since this is a Singleton
  private LanguageModel(String corpusFilePath) throws Exception {
      constructDictionaries(corpusFilePath);
  }
  
  
  public void constructDictionaries(String corpusFilePath)
          throws Exception {
  
      ...
      }
  
  // Saves the object (and all associated data) to disk
  public void save() throws Exception{
      FileOutputStream saveFile = new FileOutputStream(Config.languageModelFile);
      ObjectOutputStream save = new ObjectOutputStream(saveFile);
      save.writeObject(this);
      save.close();
  }
  
  // Creates a new lm object from a corpus
  public static LanguageModel create(String corpusFilePath) throws Exception {
      if(lm_ == null ){
          lm_ = new LanguageModel(corpusFilePath);
      }
      return lm_;
  }
  
  }
  </code></pre>
  
  <p>The class I defined is as follows:</p>
  
  <pre><code>import java.io.Serializable;
  
  import java.util.HashMap;
  
  public class Dictionary implements Serializable {
  
  private int termCount;
  private HashMap&lt;String, Integer&gt; map;
  
  public int termCount() {
      return termCount;
  }
  
  public Dictionary() {
      termCount = 0;
      map = new HashMap&lt;String, Integer&gt;();
  }
  
  ...
  }
  </code></pre>
  
  <p>When I try <code>save.writeObject(unigramDict)</code>, it can save this variable properly. Since it is a large variable, I can simply check the size of the file. It is 5MB. When I switch to <code>save.writeObject(this)</code>, the size of the file is only 53 Bytes.</p>