为什么Java抱怨“无有效的构造函数”,即使存在默认构造函数?

时间:2014-12-27 20:43:26

标签: java serialization constructor deserialization externalizable

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();
    }

  }
}

ExternalizableClassExternalizableClass.Write类都有默认(非arg)构造函数。但是在反序列化时,Java抱怨道:

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)

有谁可以解释问题是什么?

1 个答案:

答案 0 :(得分:6)

我怀疑问题是它是一个内部类...所以它实际上没有一个无参数构造函数。相反,它有两个构造函数,其中一个构造函数引用封闭类的实例,其中一个构造函数引用封闭类的实例和int

如果你通过将static修饰符添加到类声明(对于Write)来使它只是一个嵌套类,那么我怀疑它会没问题,因为那时候它不会在每个构造函数上都有隐式的额外参数。