What is java:transient and what is the effect

Source: Internet
Author: User

Reference blog:Java transient keywords

"Java's serialization provides a mechanism for persisting object instances. When persisting an object, there may be a special object data member, and we do not want to use the serialization mechanism to save it. In order to turn off serialization on a domain of a particular object, you can precede the field with the keyword transient. Transient is a keyword in the Java language that is used to indicate that a domain is not part of the serialization of that object. When an object is serialized, the value of the transient variable is not included in the serialized representation, whereas the non-transient variable is included. ”

To verify this, we write the code: set a normal property, a transient tag property, then serialize the object and then read it, and find that the properties of the transient tag are not serialized.

1  Public classMytransientImplementsjava.io.Serializable {2     /**3      * 4      */5     Private Static Final LongSerialversionuid = -8771981914596808776l;6     PrivateDate loggingdate =NewDate ();7     PrivateString uid;8     Private transientString pwd;9 Ten      Publicmytransient (string user, string password) { OneUID =user; APWD =password; -     } -  the      PublicString toString () { -String Password =NULL; -         if(pwd = =NULL) { -Password = "Not SET"; +}Else { -Password =pwd; +         } A         return"Logon info: \ n" + "User:" + uid + "\ n Logging Date:" +loggingdate.tostring () at+ "\ n Password:" +password; -     } -  -      Public Static voidMain (string[] args) { -Mytransient Loginfo =NewMytransient ("MIKE", "Mechanics"); - System.out.println (loginfo.tostring ()); in         Try { -ObjectOutputStream o =NewObjectOutputStream (NewFileOutputStream ("Loginfo.out")); to O.writeobject (loginfo); + o.close (); -ObjectInputStream in =NewObjectInputStream (NewFileInputStream ("Loginfo.out")); theMytransient LogInfo1 =(mytransient) in.readobject (); * System.out.println (loginfo1.tostring ()); $}Catch(Exception e) {Panax Notoginseng         } -}

At the end of the paper, the author reminds me of the potential problems caused by careless treatment of transient domain.

1  Public classGuestlogginginfoImplementsjava.io.Serializable {2     PrivateDate loggingdate =NewDate ();3     PrivateString uid;4     Private transientString pwd;5 6 Guestlogginginfo () {7UID = "Guest";8PWD = "Guest";9     }Ten  One      PublicString toString () { A         //Same as above -     } -}
View Code

Now, if we walk through an instance of Guestlogginginfo, write it to disk, and then read it from disk, we still see that the object being read back is printed password "not SET". When an instance of a class is read from disk, it does not actually execute the constructor of the class, but instead loads a persisted state of the class object and assigns that state to another object of the class.

Http://www.blogjava.net/fhtdy2004/archive/2009/06/20/286112.html

What is java:transient and what is the effect

Related Article

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.