众所周知,静态字段是不可序列化的,
但是我们类中的serialversionUID是final和static的,就算是static和final又如何序列化
最佳答案
在序列化过程中,写入的内容之一是类描述符。该类描述符包含类的名称和 serialVersionUID。
方法可以在类java.io.ObjectStreamClass( http://docs.oracle.com/javase/7/docs/api/java/io/ObjectStreamClass.html )中找到
/**
* Writes non-proxy class descriptor information to given output stream.
*/
void writeNonProxy(ObjectOutputStream out) throws IOException {
out.writeUTF(name);
out.writeLong(getSerialVersionUID());
byte flags = 0;
if (externalizable) {
flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;
int protocol = out.getProtocolVersion();
if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) {
flags |= ObjectStreamConstants.SC_BLOCK_DATA;
}
} else if (serializable) {
flags |= ObjectStreamConstants.SC_SERIALIZABLE;
}
if (hasWriteObjectData) {
flags |= ObjectStreamConstants.SC_WRITE_METHOD;
}
if (isEnum) {
flags |= ObjectStreamConstants.SC_ENUM;
}
out.writeByte(flags);
out.writeShort(fields.length);
for (int i = 0; i < fields.length; i++) {
ObjectStreamField f = fields[i];
out.writeByte(f.getTypeCode());
out.writeUTF(f.getName());
if (!f.isPrimitive()) {
out.writeTypeString(f.getTypeString());
}
}
}
关于java - serialversionuid 是静态的和最终的,但为什么它是可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18737639/