What does transient in Java do?

Source: Internet
Author: User

The transient keyword is used to prevent serialization of a domain. If a reference type is transient decorated, the result of deserialization is null. The base type is 0. If a class is not serializable when referencing a type, you should also use the transient adornment, which is skipped directly when deserializing.

You can use transient to modify fields that you do not want to save

The following example shows the name modified by transient, and the deserialized result is null after using a serialized clone

Package Com.lk.b;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.io.serializable;public class Employee implements cloneable,serializable{private static final long Serialversionuid = 1l;private transient string name;private int age;public Employee (String name,int age) {this.name = name; This.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} @Overridepublic String toString () {//TODO auto-generated method Stubstringbuffer sb = new StringBuffer (); Sb.append ("Name:" +name+ ","); Sb.append ("Age:" +age+ "\ n"); return sb.tostring (); @Overrideprotected Employee Clone () {//TODO auto-generated method Stubemployee Employss = null; Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); try {objectoutputstream oos = new ObjectOutputStream (BAOs); o Os.writEobject (this); Oos.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Bytearrayinputstream Bais = new Bytearrayinputstream (Baos.tobytearray ()); try {ObjectInputStream ois = new ObjectInputStream (Bais); Employss = (Employee) ois.readobject (); Ois.close ();} catch (IOException | ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return Employss;}}

  

 Package com.lk.b;  Public class TEST3 {    publicstaticvoid  main (string[] args) {        new Employee ("Akkun", +);        SYSTEM.OUT.PRINTLN (EMP);         = Emp.clone ();        System.out.println (EMP1);    }}
Name: Akkun, Age: + name:null, Age: 21

What does transient in Java do?

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.