Serializable and transient

后端 未结 5 1756
無奈伤痛
無奈伤痛 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条回答
  • 2021-01-02 07:09

    So you're asking why you can't mark a class as not serializable (like a transient member)? Why wouldn't you just not mark class members of the not-to-serialize type as transient? Or use a serialization delegate for that class type when you do the serialization? It seems a little weird that you would want to tell Java to not do something at this level instead of telling it to do something.

    0 讨论(0)
  • 2021-01-02 07:14

    Transient keywords are used to protect a variable or a field from being stored and we do this to protect some sensitive information we just don't want to distribute at every place and we use Serializable interface to make a class Serializable. Although we can use Externalizable interface also but we prefer to use Serializable because of some advantages.

    Go though this to clearly understand Serialization and transient keyword. http://www.codingeek.com/java/io/object-streams-serialization-deserialization-java-example-serializable-interface/

    0 讨论(0)
  • 2021-01-02 07:15

    Serializable is a marker interface (like Cloneable) that is used to set a flag for standard Java runtime library code that an object can be serialised according to the designer of that class.

    The transient keyword can be used to specify that an attribute does not need to be serialised, for instance because it is a derived attribute.

    See also this reply to a similar question on SO and this one about designing marker interfaces.

    Update

    Why marker interfaces and no keywords for things like serializable, cloneable, etc? My guess would be the possibility to consistently extend the Java runtime lib with new marker interfaces combined with too many keywords if behavioural aspects made it into the language.

    The fact that class attributes cannot implement Interfaces and transient can be seen as a generic property of an attribute makes sense of introducing transient as a language keyword.

    0 讨论(0)
  • 2021-01-02 07:19

    Why isn't used some special keyword to mark classes as serializable too? Serializable interface looks like a magic numbers in code and not like the language feature.

    I think you have to look at it the other way: language keywords exist mainly to support compile-time language constructs. Serialization is a runtime mechanism. Additionally, you don't want to have an extra keyword for everything, because you then can't use it as an identifier. A marker interface on the other hand is much less intrusive.

    The question is thus: why do we need a language keyword to mark transient fields? And the answer is that there simply was no other way to mark specific fields at that time.

    Nowadays, one would use annotations for this purpose in both cases (and for other things like the obscure strictfp keyword as well).

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题