ble*_*ras 3 java serialization static
我一直在读静态字段没有序列化,但经过测试后,我发现这不是真的.
静态修改器甚至会覆盖transient修改器并使该字段可序列化.
我从一本书中写了一个例子,该书显示静态瞬态场被序列化.
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
静态字段未序列化的一般概念是错误的吗?这只是推荐吗?为什么瞬态修改器不会对静态生效?
注意:我知道在构造函数中初始化静态字段是一个奇怪的代码,但编译器允许我这样做,这只是为了理解静态字段序列化.
这与序列化无关,但是由于您在创建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();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二次运行它,将main方法更改为:
public static void main(String[] args) {
// serializePresident();
deserializePresident();
}
Run Code Online (Sandbox Code Playgroud)
看看会出现什么.
对我来说,第一次运行返回:
US President [name=Barack Obama, period=2009 to --, term=56th term]
US President [name=Barack Obama, period=2009 to --, term=56th term]
Run Code Online (Sandbox Code Playgroud)
并且第二次运行返回:
US President [name=Barack Obama, period=2009 to --, term=null]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2150 次 |
| 最近记录: |