这是我的示例代码:
public class ExternalizableClass implements Externalizable
{
final int id;
public ExternalizableClass()
{
id = 0;
}
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();
}
@Override
public String toString()
{
return "id: " + id;
}
}
由于id = in.readInt();
给出了Error:(36, 5) java: cannot assign a value to final variable id
,因此无法编译。但是,我可以想到真正的用例,其中不可变字段(如id)需要外部化,而我们也希望保持其不变性。
那么解决此问题的正确方法是什么?
答案 0 :(得分:-1)
对于最终字段的概念,read函数没有多大意义,因为它被初始化的值应该永远是它的值。读取功能不应该改变它。
显然,使用public ExternalizableClass(int i)
构造函数初始化的对象不应该能够读取新值 - 如果它们可以,那么它们的id
值不是真正的最终值。我能看到的唯一另一种方法是使默认构造函数初始化一个“未读”实例,允许您稍后调用read。但是,这确实需要删除最终修饰符并解决此问题。所以这看起来像这样:
public class ExternalizableClass implements Externalizable
{
private int id;
private boolean initted;
int getId(){
return id;
}
public ExternalizableClass(int i, boolean initted){
id = i;
this.initted = initted;
}
public ExternalizableClass(){
this(0, true); //Default instances can't be changed
}
public ExternalizableClass(int i)
{
this(i, true); //Instances from this constructor can't be changed either
}
@Override
public void writeExternal(ObjectOutput out) throws RuntimeException, IOException
{
if(! initted)
throw new RuntimeException("Can't write unitialized instance, " + this);
out.writeInt(id);
}
@Override
public void readExternal(ObjectInput in) throws RuntimeException, IOException, ClassNotFoundException
{
if(initted)
throw new RuntimeException("Can't Read into already initialized object ," + this);
id = in.readInt();
initted = true;
}
@Override
public String toString()
{
if(initted) return "id: " + id;
else return "No id";
}
}