java中的transient(轉載)

          Java語言的關鍵字,用來表示一個域不是該對象串行化的一部分。當一個對象被串行化的時候,transient型變量的值不包括在串行化的表示中,然而非transient型的變量是被包括進去的。還是不大明白。
              后來,終于搜到這篇文章,寫得很詳細。Be Careful With Transient Data
              怕萬一這篇文章鏈接失效,收藏起來。Be Careful With Transient Data
            
              終于明白了。
              當串行化某個對象時,如果該對象的某個變量是transient,那么這個變量不會被串行化進去。也就是說,假設某個類的成員變量是transient,那么當通過ObjectOutputStream把這個類的某個實例保存到磁盤上時,實際上transient變量的值是不會保存的。因為當從磁盤中讀出這個對象的時候,對象的該變量會沒有被賦值。
              另外這篇文章還提到,當從磁盤中讀出某個類的實例時,實際上并不會執行這個類的構造函數,而是讀取這個類的實例的狀態,并且把這個狀態付給這個類的對象。這點我以前似乎不知道。

          Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1064847

           

           

           Be Careful With Transient Data



          (原文來自http://www.devx.com/tips/Tip/13726)

          Expertise: Intermediate
          Language: Java
          January 28, 2000
          Be Careful With Transient Data
          Java's serialization provides an elegant, and easy to use mechanism for making an object's state persistent. While controlling object serialization, we might have a particular object data member that we do not want the serialization mechanism to save.

          To turn off serialization on a certain field of an object, we tag that field of the class of our object with the Java's "transient" keyword. This, to low-level parts of the Java virtual machine, is an indication that the transient variable is not part of the persistent state of an object.

          First, let's have some backgrounder code with Java's serialization.
          Suppose we define a class as:

          public class LoggingInfo implements java.io.Serializable
          {
          private Date loggingDate = new Date();
          private String uid;
          private transient String pwd;

          LoggingInfo(String user, String password)
          {
          uid = user;
          pwd = password;
          }
          public String toString()
          {
          String password=null;
          if(pwd == null)
          {
          password = "NOT SET";
          }
          else
          {
          password = pwd;
          }
          return "logon info: "n " + "user: " + uid +
          ""n logging date : " + loggingDate.toString() +
          ""n password: " + password;
          }
          }
          Now we can create an instance of this class and serialize it, and write the serialized object to disk as in:
          LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");
          System.out.println(logInfo.toString());
          try
          {
          ObjectOutputStream o = new ObjectOutputStream(
          new FileOutputStream("logInfo.out"));
          o.writeObject(logInfo);
          o.close();
          }
          catch(Exception e) {//deal with exception}
          To read the object back, we can write
          try
          {
          ObjectInputStream in =new ObjectInputStream(
          new FileInputStream("logInfo.out"));
          LoggingInfo logInfo = (LoggingInfo)in.readObject();
          System.out.println(logInfo.toString());
          }
          catch(Exception e) {//deal with exception}
          If we run this code, we notice that the read-back object prints password as "NOT SET". This is exactly the effect we should have expected when we declared the pwd field as transient.

          Now, let's see a potential problem that careless treatment of transient fields may cause. Suppose we modify our class definition and provide default values for the transient field, say we write:

          public class GuestLoggingInfo implements java.io.Serializable
          {
          private Date loggingDate = new Date();
          private String uid;
          private transient String pwd;

          GuestLoggingInfo()
          {
          uid = "guest";
          pwd = "guest";
          }
          public String toString()
          {
          //same as above
          }
          }
          Now, if we serialize an instance of GuestLoggingInfo, write it to disk, and read it back, we still see that the read-back object prints password as "NOT SET". In effect, the process of reading back (de-serializing) totally ignores the constructor of GuestLoggingInfo. So what happened?

          The answer lies in the fact that the initialization code is not called because we are not initializing, in other words, we are not constructing a brand new object, but loading back the persistent state of an object of a class, and assigning that state to another object of the same class. Declaring the pwd field as transient, excludes the data for that

          posted on 2009-10-10 10:03 肖麥 閱讀(237) 評論(0)  編輯  收藏 所屬分類: JavaAPI

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          English

          JavaAPI

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高清| 济宁市| 杭州市| 长宁区| 高阳县| 卫辉市| 布尔津县| 师宗县| 石柱| 北宁市| 泸水县| 梁平县| 呈贡县| 紫云| 岫岩| 盐源县| 西安市| 周口市| 广昌县| 普兰店市| 阿拉善右旗| 芦溪县| 大兴区| 宜宾市| 肥城市| 阜新| 四会市| 成安县| 南丰县| 吐鲁番市| 瑞金市| 九龙城区| 慈利县| 霸州市| 来安县| 双桥区| 兴义市| 沐川县| 雷山县| 宕昌县| 边坝县|