java - What are the conditions for Serializable? -
suppose have java class, myclass
, not inherited class. myclass
implements serializable
.
can myclass
serializable
no other conditions? depend on objects myclass
contain, , if these objects serializable
themselves?
for example in class below, if mappedoodle2
not implement serializable
, exception when mappedoodle
serialized?
import java.io.serializable; public class mappedoodle implements serializable { private static final long serialversionuid = -1760231235147491826l; private string text; private int value; private mappedoodle2 mm = new mappedoodle2(); public mappedoodle(string text, int value) { this.text = text; this.value = value; } public string gettext() { return text; } public int getvalue() { return value; } @override public string tostring() { return text + ", " + value; } }
to serialize object, i'm assuming using objectoutputstream
. here's relevant documentation: http://docs.oracle.com/javase/7/docs/api/java/io/objectoutputstream.html#writeobject(java.lang.object)
that section describes basics of how objects written. stream writes following:
- the class object is
- the signature (
public class mappedoodle implements serializable
) - all fields neither
transient
norstatic
each field object written stream. means each field must implement serializable
. if doesn't, notserializableexception
.
you have couple of options making work:
- make
mappedoodle2
serializable - make field
mm
transient
, means won't serialized.
Comments
Post a Comment