title: Why Java complains "No Valid Constructor" even when default constructor is present?
body: <pre><code>public class ExternalizableClass implements Externalizable
  {
    public static ExternalizableClass CACHE = new ExternalizableClass(-1);
  
    int id;
  
    public ExternalizableClass()
    {
      id = (int)(Math.random() * 1000);
    }
  
    public ExternalizableClass(int i)
    {
      id = i;
    }
  
  
    @Override
    public void writeExternal(ObjectOutput out) throws IOException
    {
      out.writeInt(id);
    }
  
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
    {
      //id = in.readInt();
      id = in.readInt();
    }
  
  
    public Object writeReplace() throws ObjectStreamException
    {
      return new Write(0);
    }
  
    private class Write extends ExternalizableClass
    {
      int value;
  
      public Write()
      {
      }
  
      public Write(int i)
      {
        value = i;
      }
  
      public Object readResolve() throws ObjectStreamException
      {
        return ExternalizableClass.CACHE;
      }
  
  
    }
  
    @Override
    public String toString()
    {
      return "id: " + id;
    }
  
    public static void main(String[] argv)
    {
      try
      {
        ExternalizableClass ex = ExternalizableClass.CACHE;
  
        System.out.println(ex);
  
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("temp.txt"));
        oos.writeObject(ex);
  
        oos.close();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
  
      ExternalizableClass ex;
      try
      {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("temp.txt"));
        ex = (ExternalizableClass) ois.readObject();
  
        System.out.println(ex);
  
        ois.close();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
  
    }
  }
  </code></pre>
  
  <p>Both <code>ExternalizableClass</code> and <code>ExternalizableClass.Write</code> class have default (non-arg) constructors. But when deserializing it, Java complains: </p>
  
  <pre><code>java.io.InvalidClassException: SeralizableTest.ExternalizableClass$Write; no valid constructor
      at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:150)
      at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:768)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1772)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at SeralizableTest.ExternalizableClass.main(ExternalizableClass.java:124)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  </code></pre>
  
  <p>Can anyone explain what the problems are? </p>