简体   繁体   English

在Serializable类中使用transient关键字

[英]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 the ObjectOutputStream . 因为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.

相关问题 可序列化类中的不可序列化字段(transient 关键字) - Non-serializable field in serializable class (transient keyword) 使“class”瞬态或可序列化但该类是可序列化的 - Make “class” transient or serializable BUT the class is serializable “可序列化”类中的字段应该是瞬态的或可序列化的 - Fields in a “Serializable” class should either be transient or serializable 可以为 Serializable 类设置锁瞬态吗? - Is it okay to to make the lock transient for a Serializable class? 可序列化和瞬态 - Serializable and transient SonarQube-“可序列化”类中的规则字段应该是瞬态的或可序列化的 - SonarQube - Rule Fields in a “Serializable” class should either be transient or serializable 关键字为“ transient”的不可序列化注入bean的序列化过程 - serialization process of non serializable injected bean with keyword 'transient' 可序列化类中的非瞬态不可序列化实例字段 - Non-transient non-serializable instance field in serializable class 非实体变量:“可序列化”class 中的字段应该是瞬态的或可序列化的 - Non entity variables: Fields in a “Serializable” class should either be transient or serializable 为什么google-collection AbstractMultimap类对成员变量使用瞬态关键字? - Why google-collections AbstractMultimap class use transient keyword for member variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM