我一直在读静态字段没有序列化,但经过测试后,我发现这不是真的。
静态修改器甚至会覆盖瞬态修改器并使该字段可序列化。
我从一本书中写了一个例子,该书显示静态瞬态字段是序列化的。
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
}
}
}
静态字段未序列化的一般概念是错误的吗?这只是推荐吗? 为什么瞬态修饰符不会对静态生效?
注意:我知道在构造函数中初始化静态字段是一个奇怪的代码,但编译器允许我这样做,这只是为了理解静态字段序列化。
答案 0 :(得分:9)
这与序列化无关,但由于您在创建usPresident变量时设置了静态字段。这将为该JVM的类设置字段。尝试在不同的程序中读取序列化的总统,并看到瞬态字段未被序列化。
顺便说一句:考虑不要忽视你的例外。
例如,重构后,您的代码可能如下所示:
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();
}
}
}
}
}
第二次运行它,将主方法更改为:
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]