Transient has "temporary", "ephemeral" meanings, we have learned about Serializable,java serialization, and when we don't want to serialize it for certain variables, we can set this variable to transient,transient is the Java language keyword To indicate that a domain is not part of the serialization of the object. Transient indicates that a property is temporary and will not be serialized.
public class Transientdemo implements serializable{/** * */private static final long serialversionuid = 1L; private transient String name; private String password; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public String GetPassword () {return password; } public void SetPassword (String password) {this.password = password; }public static void Main (string[] args) throws FileNotFoundException, IOException, classnotfoundexception {//TODO auto-g enerated method Stub String path= "D:" +file.separator+ "Object.txt"; File File=new file (path); Transientdemo transientdemo=new Transientdemo (); Transientdemo.setname ("name"); Transientdemo.setpassword ("password"); ObjectOutput output=new ObjectOutputStream (new FileOutputStream (file)); Output.writeobject (Transientdemo); ObjectInput input=new ObjectInputStream (new FileInputStream (file)); Transientdemo demo= (TransiEntdemo) Input.readobject (); System.out.println (Demo.getname () +demo.getpassword ()); }}
Result name output is null
Java-transient Summary