Java: What can and what can't be serialized?

前端 未结 9 992
Happy的楠姐
Happy的楠姐 2020-12-25 11:48

If the Serializable interface is just a Marker-Interface that is used for passing some-sort of meta-data about classes in java - I\'m a bit confused:

After reading t

9条回答
  •  眼角桃花
    2020-12-25 12:23

    More practically, no object can be serialized (via Java's built-in mechanism) unless its class implements the Serializable interface. Being an instance of such a class is not a sufficient condition, however: for an object to be successfully serialized, it must also be true that all non-transient references it holds must be null or refer to serializable objects. (Do note that that is a recursive condition.) Primitive values, nulls, and transient variables aren't a problem. Static variables do not belong to individual objects, so they don't present a problem either.

    Some common classes are reliably serialization-safe. Strings are probably most notable here, but all the wrapper classes for primitive types are also safe. Arrays of primitives are reliably serializable. Arrays of reference types can be serialized if all their elements can be serialized.

提交回复
热议问题