Serializable and transient

后端 未结 5 1777
無奈伤痛
無奈伤痛 2021-01-02 06:24

To make a class serializable we do the following:

class A implements Serializable {
    transient Object a;
}

And not this:



        
5条回答
  •  旧时难觅i
    2021-01-02 07:19

    Serializable is a marker interface. Interfaces are a standard way (in Java and in some other languages) of indicating features of a class; an "is a" relaionship. Making Serializable an interface means we can declare methods that accept or return Serializables just like we can methods that work with other interfaces. Anything else would have required syntax changes to the language (at the time; now we have annotations, but I think an interface would still be used).

提交回复
热议问题