作用:

transient关键字可以让实现了Serializable接口的类中的属性不被序列化。
就是哪个属性加了transient,他就不能被序列化。
代码证明:
bean:

  1. public class User implements Serializable{
  2. private String name;
  3. private transient int age;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String tname) {
  8. name = tname;
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void setAge(int age) {
  14. this.age = age;
  15. }
  16. }

主函数:

  1. import java.io.*;
  2. public class MainTest {
  3. public static void main(String args[]) {
  4. //初始化用户
  5. User user=new User();
  6. user.setAge(18);
  7. user.setName("ABC");
  8. System.out.println("read before Serializable");
  9. System.out.println("name: "+user.getName());
  10. System.out.println("age: "+user.getAge());
  11. File file=new File("D:/user.txt");
  12. if (file.exists()){
  13. System.out.println("\nfile delete? : "+file.delete());
  14. }
  15. //序列化
  16. try {
  17. ObjectOutputStream objo=new ObjectOutputStream(new FileOutputStream("D:/user.txt"));
  18. objo.writeObject(user);;
  19. objo.flush();
  20. objo.close();
  21. }catch (Exception e){
  22. e.printStackTrace();
  23. }
  24. //反序列化
  25. try {
  26. ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream("D:/user.txt"));
  27. User abc= (User) inputStream.readObject();
  28. System.out.println("\nread after Serializable");
  29. System.out.println("name: "+abc.getName());
  30. System.out.println("age: "+abc.getAge());
  31. }catch (Exception e){
  32. e.printStackTrace();
  33. }
  34. }
  35. }

打印:

  1. read before Serializable
  2. name: ABC
  3. age: 18
  4. file delete? : true
  5. read after Serializable
  6. name: ABC
  7. age: 0

在这里的private transient int age;就没有序列化成功。

此外,static变量修饰的静态变量,也不能被Serializable序列化。我修改了name属性为static变量,重新测试:

  1. public class MainTest {
  2. public static void main(String args[]) {
  3. //初始化用户
  4. User user=new User();
  5. user.setAge(18);
  6. User.setName("ABC");
  7. System.out.println("read before Serializable");
  8. System.out.println("name: "+user.getName());
  9. System.out.println("age: "+user.getAge());
  10. File file=new File("D:/user.txt");
  11. if (file.exists()){
  12. System.out.println("\nfile delete? : "+file.delete());
  13. }
  14. //序列化
  15. try {
  16. ObjectOutputStream objo=new ObjectOutputStream(new FileOutputStream("D:/user.txt"));
  17. objo.writeObject(user);;
  18. objo.flush();
  19. objo.close();
  20. }catch (Exception e){
  21. e.printStackTrace();
  22. }
  23. //反序列化
  24. try {
  25. //在这里改变User的类变量Name属性
  26. User.setName("DEF");
  27. ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream("D:/user.txt"));
  28. User abc= (User) inputStream.readObject();
  29. System.out.println("\nread after Serializable");
  30. System.out.println("name: "+abc.getName());
  31. System.out.println("age: "+abc.getAge());
  32. }catch (Exception e){
  33. e.printStackTrace();
  34. }
  35. }
  36. }

打印:

  1. read before Serializable
  2. name: ABC
  3. age: 18
  4. file delete? : true
  5. read after Serializable
  6. name: DEF
  7. age: 0

本应该name还是ABC的,但是却出来了DEF,说明这里的name根本不能被序列化,我们读取的是存在内存中的静态变量。