【问题标题】:why static transient field is serialized?为什么静态瞬态字段被序列化?
【发布时间】:2013-12-17 23:07:10
【问题描述】:

我一直在读到静态字段没有序列化,但经过测试,我发现这不是真的。

静态修饰符甚至覆盖瞬态修饰符并使字段可序列化。

我从一本书中写了一个例子,表明静态瞬态字段是序列化的。

import java.io.*;

class USPresident implements Serializable {

    private static final long serialVersionUID = 1L;

    @Override
    public String toString() {
        return "US President [name=" + name
                + ", period=" + period + ", term=" + term + "]";
    }

    public USPresident(String name, String period, String term) {
        this.name = name;
        this.period = period;
        this.term = term;
    }
    private String name;
    private String period;
    private  static transient  String term;
}

class TransientSerialization {

    public static void main(String[] args) {
        USPresident usPresident = new USPresident("Barack Obama", "2009 to --", "56th term");
        System.out.println(usPresident);
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("USPresident.data"))) {
            oos.writeObject(usPresident);
        } catch (IOException ioe) {
// ignore
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("USPresident.data"))) {
            Object obj = ois.readObject();
            if (obj != null && obj instanceof USPresident) {
                USPresident presidentOfUS = (USPresident) obj;
                System.out.println(presidentOfUS);

            }
        } catch (IOException ioe) {
// ignore
        } catch (ClassNotFoundException e) {
// ignore
        }
    }
}

静态字段不序列化的一般概念是错误的吗?仅仅是推荐吗? 为什么瞬态修饰符对 static 不起作用?

注意:我知道在构造函数中初始化静态字段是一个奇怪的代码,但编译器让我这样做,这只是为了理解静态字段序列化。

【问题讨论】:

  • 您的代码并不能证明任何事情:类的所有实例的静态值都是相同的,因此它不会改变是正常的...尝试读取您的文件而不用相同的方式写入之前的节目。

标签: java serialization static


【解决方案1】:

这与序列化无关,而是因为您在创建 usPresident 变量时设置了静态字段。这将为该 JVM 的 class 设置字段。尝试在不同的程序中读取序列化的总统,看看瞬态字段没有序列化。

顺便说一句:考虑不要忽略您的例外情况。


例如,重构后,您的代码可能如下所示:

class USPresident implements Serializable {

   private static final long serialVersionUID = 1L;

   @Override
   public String toString() {
      return "US President [name=" + name + ", period=" + period + ", term="
            + term + "]";
   }

   public USPresident(String name, String period, String term) {
      this.name = name;
      this.period = period;
      this.term = term;
   }

   private String name;
   private String period;
   private static transient String term;
}

class TransientSerialization {

   public static void main(String[] args) {
      serializePresident();
      deserializePresident();
   }

   private static void deserializePresident() {
      ObjectInputStream ois = null;
      try {
         ois = new ObjectInputStream(new FileInputStream(
               "USPresident.data"));
         Object obj = ois.readObject();
         if (obj != null && obj instanceof USPresident) {
            USPresident presidentOfUS = (USPresident) obj;
            System.out.println(presidentOfUS);

         }
      } catch (IOException ioe) {
         ioe.printStackTrace();
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } finally {
         if (ois != null) {
            try {
               ois.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }

   private static void serializePresident() {
      USPresident usPresident = new USPresident("Barack Obama", "2009 to --",
            "56th term");
      System.out.println(usPresident);
      ObjectOutputStream oos = null;
      try {
         oos = new ObjectOutputStream(new FileOutputStream("USPresident.data"));
         oos.writeObject(usPresident);
         oos.close();
      } catch (IOException ioe) {
         ioe.printStackTrace();
      } finally {
         if (oos != null) {
            try {
               oos.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}

第二次运行时,将 main 方法更改为:

   public static void main(String[] args) {
      // serializePresident();
      deserializePresident();
   }

看看会发生什么。

对我来说,第一次运行返回:

US President [name=Barack Obama, period=2009 to --, term=56th term]
US President [name=Barack Obama, period=2009 to --, term=56th term]

第二次运行返回:

US President [name=Barack Obama, period=2009 to --, term=null]

【讨论】:

  • 正确。我检查了文件,静态或瞬态字段未序列化。我读错了代码并认为它​​是一个序列化点。
  • @bleras:很高兴你已经弄清楚了。我已经发布了代码来证明这一点。
  • 是的,谢谢新代码,做更多测试很有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2010-10-28
相关资源
最近更新 更多