`

Be Careful With Transient Data(Java序列化与trasient变量)

阅读更多

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 field from the persistent state of our object. Then, upon de-serialization, since there is no data preserved for the pwd field, the field gets Java's default value for its type (null for String).

So, if you mark a field of an object as transient, and write that object to disk, expect to have the default value of the type of that field when you de-serialize the object, and not the actual value that the field had before its state was serialized. If a default value (or any meaningful value) is essential for a transient field of a de-serialized object, you have to assign it yourself either directly (if the field is public) or via a setter method.


Behrouz Fallahi

分享到:
评论

相关推荐

    Java序列化_Java序列化结构_

    Java序列化是Java平台中的一种持久化机制,它允许对象的状态被转换成字节流,以便存储、网络传输或在不同时间点恢复。这个过程被称为序列化,而反向操作称为反序列化。序列化在许多场景下都非常有用,比如在分布式...

    Java序列化

    Java序列化是Java平台中的一种标准机制,允许将对象的状态转换为字节流,以便存储在磁盘上、通过网络进行传输或者在某些时候恢复原来的对象状态。这一过程包括两个主要步骤:对象的序列化(将对象转换为字节流)和反...

    java 序列化时排除指定属性

    在Java编程中,序列化是将对象的状态转换为字节流的过程,以便可以存储或在网络上传输。这个过程使得对象可以在不同的时间或者不同的系统之间进行恢复。然而,有时候我们不希望序列化对象的所有属性,可能是因为某些...

    java serializable 序列化与反序列化

    Java的序列化与反序列化是Java开发中的一项重要技术,它允许我们将对象的状态转换为字节流,以便存储或在网络上传输。`Serializable`接口是Java提供的一个标记接口,用于实现对象的序列化。当一个类实现了这个接口,...

    07-Java序列化面试题(10题)-新增.pdf

    下面是 10 个与 Java 序列化相关的面试题目: 1. 什么是 Java 序列化,如何实现 Java 序列化? Java 序列化是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。为了实现 Java 序列化,需要将需要...

    java 对象的序列化与反序列化

    Java对象的序列化和反序列化是Java编程中一项重要的技术,主要用于将对象的状态转换为字节流,以便存储或在网络上传输。这一过程对于理解Java的IO操作、持久化数据以及实现分布式通信等场景非常关键。 首先,我们来...

    java序列化全解

    Java序列化是Java平台中的一种核心机制,它允许对象的状态被转换成字节流,以便存储到磁盘、数据库,或者在网络中进行传输。这对于实现持久化、远程方法调用(RMI)以及Enterprise JavaBeans(EJB)等高级功能至关...

    通过实例深入了解java序列化

    静态变量序列化 ---------------- 在 Java 序列化中,静态变量不会被序列化。这是因为静态变量是属于类的,而不是对象的。例如,清单 2 中,静态变量 staticVar 的值在序列化后被修改,但是读取对象时,静态变量的...

    java 序列化代码示例

    Java序列化是Java平台中的一种标准机制,它允许将对象的状态转换为字节流,以便存储、传输或恢复。在Java中,一个类如果要实现序列化,需要实现`Serializable`接口,这是一个标记接口,不包含任何方法。下面我们将...

    Java_序列化的高级认识

    #### 静态变量序列化 通常认为,静态变量不属于对象状态的一部分,因此不会被序列化。但在某些情况下,序列化机制会保存静态字段的值,这主要取决于序列化过程的具体实现。为了防止不必要的序列化,可以通过在类...

    java序列化实现演示

    Java序列化是Java平台中的一种标准机制,允许对象的状态被保存到磁盘或者在网络中进行传输,以便在后续的时间或地点恢复这些对象。这个过程包括两个主要操作:序列化(将对象转换为字节流)和反序列化(将字节流恢复...

    Java序列化的机制和原理

    Java的序列化机制允许开发者在类中定义`readObject()`和`writeObject()`方法来自定义序列化和反序列化的行为,或者使用`transient`关键字来排除某些字段不参与序列化。 总之,Java序列化是一个强大的工具,它使得...

    java自动序列化

    对于类中包含的transient和static修饰的成员变量,它们不会参与序列化过程,因为transient变量表示临时状态,static变量表示类级别的状态,不依赖于对象实例。 四、序列化过程 1. 创建ObjectOutputStream实例,如`...

    深入理解Java虚拟机-Java内存区域透彻分析(序列化、反序列化概念及其使用场景+实现序列化的方式+transient关键字)

    深入理解Java虚拟机-Java内存区域透彻分析(序列化、反序列化概念及其使用场景+实现序列化的方式+transient关键字) Java序列化和反序列化是Java虚拟机中的一种重要机制,它们可以将Java对象转换为二进制数据,然后...

    java对象序列化.ppt

    需要注意的是,即使`transient`变量不被序列化,但它的引用如果指向一个可序列化的对象,那么这个对象仍会被序列化。 总结来说,Java对象序列化是将对象状态转换为字节流,便于存储和网络传输的关键技术。通过实现`...

    Java实现序列化例子

    如果某些变量不希望被序列化,可以使用`transient`关键字标记它们。 - **安全性**:序列化可能导致安全问题,因为任何能读取序列化文件的人都可以构造对象并执行其方法。因此,应谨慎处理序列化数据,特别是在网络...

    Java对象序列化标准最新版

    ### Java对象序列化标准知识点详解 #### 一、系统架构概览 **1.1 概览** Java 对象序列化是一种将Java对象的状态转换成字节流的过程,以便于在网络上传输或存储到磁盘上。Java序列化标准定义了一套规则来描述如何...

    java序列化与反序列化

    Java序列化是将Java对象转换为字节流的过程,以便可以在磁盘、数据库或网络上存储或传输这些对象。这使得我们能够保存对象的状态,并在稍后的时间点恢复它,或者在网络之间传递对象。反序列化是相反的过程,即从字节...

    java对象序列化和反序列化

    Java对象序列化与反序列化是Java编程中重要的概念,主要应用于数据持久化、网络传输以及存储等场景。本文将详细解析这两个概念及其在实际应用中的实现方式。 **一、Java对象序列化** 1. **定义**: Java对象序列化...

    Java transient关键字与序列化操作实例详解

    Java transient关键字与序列化操作实例详解 Java语言中的transient关键字是用来修饰变量的,表明该变量不会被Java虚拟机(JVM)序列化,即不会被写入到二进制流中。序列化是Java语言中的一种机制,用于将对象转换为...

Global site tag (gtag.js) - Google Analytics