先看例子:html
demo1java
package com.caiya.test._transient; import java.io.Serializable; /** * Created by caiya on 16/3/31. */ public class User implements Serializable { private static final long serialVersionUID = -5627047021580508291L; private Long userId; public static String userName; private transient String password; public User() { } public User(Long userId, String userName, String password) { this.userId = userId; this.userName = userName; this.password = password; } public Long getUserId() { return userId; } public User setUserId(Long userId) { this.userId = userId; return this; } public String getPassword() { return password; } public String getUserName() { return userName; } public User setPassword(String password) { this.password = password; return this; } public User setUserName(String userName) { this.userName = userName; return this; } }
package com.caiya.test._transient; import com.alibaba.fastjson.JSON; import java.io.*; /** * Created by caiya on 16/3/31. */ public class TransientTest { public static void main(String[] args) throws IOException, ClassNotFoundException { TransientTest transientTest = new TransientTest(); transientTest.test1(); User.userName = "lisi"; transientTest.test2(); } public void test1() throws IOException { User user = new User(1L, "zhangsan", "111111"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("/Users/caiya/workspace/test/transient.txt"))); objectOutputStream.writeObject(user); objectOutputStream.flush(); objectOutputStream.close(); } public void test2() throws IOException, ClassNotFoundException { ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("/Users/caiya/workspace/test/transient.txt"))); Object object = objectInputStream.readObject(); System.out.print(JSON.toJSONString(object)); } }
輸出結果:json
{"userId":1,"userName":"lisi"}
demo2this
package com.caiya.test._transient; import java.io.*; /** * Created by caiya on 16/3/31. */ public class User2 implements Externalizable { private static final long serialVersionUID = 127908457926642843L; private Long userId; public static String userName = "default"; private transient String password; public User2() { } public User2(Long userId, String userName, String password) { this.userId = userId; this.userName = userName; this.password = password; } public Long getUserId() { return userId; } public User2 setUserId(Long userId) { this.userId = userId; return this; } public String getPassword() { return password; } public String getUserName() { return userName; } public User2 setPassword(String password) { this.password = password; return this; } public User2 setUserName(String userName) { this.userName = userName; return this; } public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(this.getUserName()); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { userName = (String) in.readObject(); } }
package com.caiya.test._transient; import com.alibaba.fastjson.JSON; import java.io.*; /** * Created by caiya on 16/3/31. */ public class TransientTest { public static void main(String[] args) throws IOException, ClassNotFoundException { TransientTest transientTest = new TransientTest(); transientTest.test1(); User2.userName = "lisi"; transientTest.test2(); } public void test1() throws IOException { User2 user = new User2(1L, "zhangsan", "111111"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("/Users/caiya/workspace/test/transient.txt"))); objectOutputStream.writeObject(user); objectOutputStream.flush(); objectOutputStream.close(); } public void test2() throws IOException, ClassNotFoundException { ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("/Users/caiya/workspace/test/transient.txt"))); Object object = objectInputStream.readObject(); System.out.print(JSON.toJSONString(object)); } }
輸出結果:spa
{"userName":"zhangsan"}
總結code
一、java對象序列化能夠實現java.io.Serializable或java.io.Externalizable接口;htm
二、使用transient修飾的變量不能被序列化;對象
三、使用static關鍵字默認保存在內存中,若是須要序列化,能夠經過重寫java.io.Externalizable的writeExternal/readExternal方法接口