Java Foundation-serializable

Source: Internet
Author: User

1 @Test2      Public voidTestserializable ()throwsIOException, ClassNotFoundException {3Son son =NewSon ();4 Son.changefieldvalue ();5Bytearrayoutputstream Bytearrayoutputstream =NewBytearrayoutputstream ();6ObjectOutputStream ObjectOutputStream =NewObjectOutputStream (bytearrayoutputstream);7 Objectoutputstream.writeobject (son);8Son.staticfield = 3;9ObjectInputStream ObjectInputStream =NewObjectInputStream (NewBytearrayinputstream (Bytearrayoutputstream.tobytearray ()));Tenson =(Son) objectinputstream.readobject (); One         /** A * Output: - * Parent - * Son the * Parent - * son{staticfield=3, s=2, Transientfield=0, p=1} -          */ - System.out.println (son); +     } -     Private Static classSonextendsParentImplementsserializable{ +         //Normal serialization A         Private ints = 1; at         //class fields do not participate in serialization -         Private Static intStaticfield = 1; -         //transient field does not participate in serialization -         Private transient intTransientfield = 1; -         //because subclasses implement serializable, the subclass fields are serialized and are not initialized with the declared constructor when deserializing . -         //instead of instantiating an empty object directly with the JVM reflection attribute and assigning it, the constructor for the subclass can have no public, no argument constructors.  in         PrivateSon () { -System.out.println ("Son"); to         } +         Private voidChangefieldvalue () { -              This. S = 2; theSon.staticfield = 2; *              This. Transientfield = 2; $              This. P = 2;Panax Notoginseng         } -  the @Override +          PublicString toString () { A             return"Son{" + the"staticfield=" + Staticfield + +", s=" + S + -", transientfield=" + Transientfield + $", p=" + p + $‘}‘; -         } -     } the     Private Static classparent{ -         //classes that do not implement serializable, fields do not participate in serializationWuyi         protected intp = 1; the         //because the parent does not implement serializable, the fields Here are not serialized, -         //When deserializing a subclass, because the parent class is not serialized, it is necessary to call the constructor of the parent class to initialize the parent class. Wu         //the parent class must have a public, non-parametric constructor.  -          PublicParent () { AboutSystem.out.println ("Parent"); $         } -}

Java Foundation-serializable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.