Java中的transient关键字

转载于:[lfsf802](http://blog.csdn.net/lfsf802/article/details/43239663)

关键字介绍

 

一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java的这种序列化模式为开发者提供了很多便利,可以不必关系具体序列化的过程,只要这个类实现了Serilizable接口,这个的所有属性和方法都会自动序列化。但是有种情况是有些属性是不需要序列号的,所以就用到这个关键字。只需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。

 

代码:

 

实现Serializable接口的UserInfo类,并且有一个transient关键字修饰的属性

 

  1. package com.testtransient.model;  
  2. import java.io.Serializable;  
  3. public class UserInfo implements Serializable {  
  4.    
  5.         private static final long serialVersionUID = 1L;  
  6.    
  7.         private String name ;  
  8.          
  9.         private transient String pwd ;  
  10.          
  11.         public UserInfo(String name,String pwd){  
  12.                this.name =name;  
  13.                this.pwd =pwd;  
  14.        }  
  15.          
  16.         public String toString(){  
  17.                return “name=” +name +“,psw=” +pwd ;  
  18.        }  
  19. }  
package com.testtransient.model;
import java.io.Serializable;
public class UserInfo implements Serializable {

        private static final long serialVersionUID = 1L;

        private String name ;

        private transient String pwd ;

        public UserInfo(String name,String pwd){
               this.name =name;
               this.pwd =pwd;
       }

        public String toString(){
               return "name=" +name +",psw=" +pwd ;
       }
}



通过输入输出流编写的测试程序

 

  1. public class TestTransient {  
  2.    
  3.         public static void main(String[] args) {  
  4.               UserInfouserInfo = new UserInfo(“张三” , “123456” );  
  5.               System. out.println(userInfo);  
  6.                try {  
  7.                       // 序列化,被设置为transient的属性没有被序列化  
  8.                      ObjectOutputStreamo = new ObjectOutputStream(new FileOutputStream(  
  9.                                    ”UserInfo.out”));  
  10.                      o.writeObject(userInfo);  
  11.                      o.close();  
  12.               } catch (Exception e) {  
  13.                      e.printStackTrace();  
  14.               }  
  15.                try {  
  16.                       // 重新读取内容  
  17.                      ObjectInputStream in = new ObjectInputStream( new FileInputStream(  
  18.                                    ”UserInfo.out”));  
  19.                      UserInforeadUserInfo = (UserInfo) in.readObject();  
  20.                       // 读取后psw的内容为null  
  21.                      System. out.println(readUserInfo.toString());  
  22.               } catch (Exception e) {  
  23.                      e.printStackTrace();  
  24.               }  
  25.        }  
public class TestTransient {

        public static void main(String[] args) {
              UserInfouserInfo = new UserInfo("张三" , "123456" );
              System. out.println(userInfo);
               try {
                      // 序列化,被设置为transient的属性没有被序列化
                     ObjectOutputStreamo = new ObjectOutputStream(new FileOutputStream(
                                   "UserInfo.out"));
                     o.writeObject(userInfo);
                     o.close();
              } catch (Exception e) {
                     e.printStackTrace();
              }
               try {
                      // 重新读取内容
                     ObjectInputStream in = new ObjectInputStream( new FileInputStream(
                                   "UserInfo.out"));
                     UserInforeadUserInfo = (UserInfo) in.readObject();
                      // 读取后psw的内容为null
                     System. out.println(readUserInfo.toString());
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }


运行结果:




从上面结果能够看出来经过transient关键字修饰的字段是不能够被序列化的。


原文地址:https://www.cnblogs.com/Arry10/p/7731752.html