肖麦
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
肖麦
阅读(228)
评论(0)
编辑
收藏
所属分类:
JavaAPI
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
相关文章:
java中的transient(转载)
Java中finally关键字的使用
Java中"异常机制"的深入研究
java 日期格式化(转载)
java文件操作(一)
java.util包学习(二)集合框架(接口)
java.util包学习(一)类与接口结构图
Powered by:
BlogJava
Copyright © 肖麦
<
2024年11月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
导航
BlogJava
首页
新随笔
联系
聚合
管理
统计
随笔 - 2
文章 - 14
评论 - 11
引用 - 0
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(2)
给我留言
查看公开留言
查看私人留言
随笔分类
人生格言(2)
(rss)
随笔档案
2007年6月 (1)
2007年4月 (1)
文章分类
AJAX(1)
(rss)
Hibernate
(rss)
JavaAPI(7)
(rss)
JSP
(rss)
Linux(1)
(rss)
MySql(1)
(rss)
Spring(2)
(rss)
Struts
(rss)
Tabestry
(rss)
Webwork
(rss)
编码问题(1)
(rss)
文章档案
2009年10月 (2)
2009年9月 (1)
2009年5月 (1)
2008年3月 (4)
2007年6月 (1)
2007年4月 (3)
2007年3月 (2)
English
JavaAPI
Eclipse下的Java反编译插件:Jode Decompiler
java 反射
(rss)
javascript校验总结
(rss)
java泛型
搜索
最新评论
1. re: jQuery load()方法用法集锦!
东风公司
--高峰时段
2. re: jQuery load()方法用法集锦!
评论内容较长,点击标题查看
--三才
3. re: jQuery load()方法用法集锦![未登录]
sssss
--aaa
4. re: jQuery load()方法用法集锦!
淡淡道
--000
5. re: jQuery load()方法用法集锦!
如何获取加载页面的高度呢?
--丸子
阅读排行榜
1. 值得珍藏的81句生活哲理名言 (18733)
2. 给自己的四个问题(435)
评论排行榜
1. 值得珍藏的81句生活哲理名言 (3)
2. 给自己的四个问题(0)