[英]Use transient keyword in not Serializable class
Would it make sense to use the transient
keyword in a class that does not implement Serializable
? 在没有实现Serializable
的类中使用transient
关键字是否有意义?
Because classes that do not implement Serializable
could still be serialized by the ObjectOutputStream
. 因为ObjectOutputStream
仍然可以序列化未实现Serializable
类。
You may want to still mark it as transient for several reasons. 由于多种原因,您可能仍希望将其标记为瞬态。 Two that immediately come to mind are: 立刻想到的两个是:
It can help convey the semantic purpose of the field. 它可以帮助传达该领域的语义目的。 Ie, it helps anyone reading your code understand what the variable to meant to be for 也就是说,它可以帮助任何阅读代码的人理解变量的含义
Third party libraries may make use of the keyword. 第三方库可以使用该关键字。 For example, Google's Gson library can serialize/deserialize any object to/from JSON, regardless of whether it implements Serializable. 例如,Google的Gson库可以将任何对象序列化/反序列化为JSON,无论它是否实现Serializable。 In this case, Gson will (by default) skip fields marked as transient. 在这种情况下,Gson将(默认情况下)跳过标记为瞬态的字段。
Because object serialization is more complex than a simple implementations of Serializable (think about proxy: proxied object can implements Serializable, but not original object and in your code you still using original class) 因为对象序列化比Serializable的简单实现更复杂(考虑代理:代理对象可以实现Serializable,但不是原始对象,在代码中你仍然使用原始类)
Another way to implements serialization is Externalizable interface to have total control of your object serialization or (from javadoc): 实现序列化的另一种方法是Externalizable接口,可以完全控制对象序列化或(来自javadoc):
Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: 在序列化和反序列化过程中需要特殊处理的类必须使用这些精确签名实现特殊方法:
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException;
private void writeObject(java.io.ObjectOutputStream stream) throws IOException
private void readObjectNoData() throws ObjectStreamException;
look at ObjectOutputStream javadoc for more information about serialization process/mechanism. 有关序列化过程/机制的更多信息,请查看ObjectOutputStream javadoc 。
EDIT: to answer your question, transient is a keyword used only in serialization context so a not Serializable
object with a transient field doesn't make sense 编辑:回答你的问题,transient是一个仅在序列化上下文中使用的关键字,所以一个不具有瞬态字段的Serializable
对象没有意义
Because classes that do not implement
Serializable
could still be serialized by theObjectOutputStream
. 因为ObjectOutputStream
仍然可以序列化未实现Serializable
类。
That is incorrect. 那是不对的。 That would throw a NotSerializableException
. 这将抛出NotSerializableException
。
The reason writeObject()
takes an Object
instead of Serializable
is that the signature comes from implementing the interface ObjectOutput
which is defined independent of serialization. writeObject()
接受Object
而不是Serializable
是签名来自实现接口ObjectOutput
,该接口是独立于序列化定义的。 But, it then prevents ObjectOutputStream
from changing its signature. 但是,它会阻止ObjectOutputStream
更改其签名。
public interface ObjectOutput {
// ...
void writeObject(Object obj);
}
通常,如果超类实现Serializable
它的子类也是可序列化的。这是唯一可以使字段瞬态并且类不能直接实现Serializable的情况,但除此之外,如果类不可序列化,则没有意义使他们瞬间。
No, It would not make sense. 不,这没有意义。
class State {
// The 'transient' modifier has no effect here because
// the 'State' class does not implement 'Serializable'.
private transient int[] stateData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.