java 中 transient 关键字意义
来源:互联网 发布:define在c语言中的意思 编辑:程序博客网 时间:2024/05/16 01:10
译文出处:Why does Java have transient variables?
另外推荐一篇文章,我觉得写得挺好的Java深度历险(十)——Java对象序列化与RMI
java 中的 transient 关键字表明了 transient 变量不应该被序列化(transient)。
参考Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields:
被 transient 标记的这些变量,表明他们是某个对象中不需要被持久状态的部分(Variables may be marked transient to indicate that they are not part of the persistent state of an object.)。
例如,你可能有一些字段是从其他字段导出来的,这些字段的值应该是程序根据一定的算法自动生成的,而不是通过序列化来持久状态(For example, you may have fields that are derived from other fields, and should only be done so programmatically, rather than having the state be persisted via serialization.)。
下面是一个相册 GalleryImage 类,这个相册类包含一张图片和由这张图片导出的一张缩略图。
class GalleryImage implements Serializable{ private Image image; private transient Image thumbnailImage; private void generateThumbnail() { // Generate thumbnail. // 根据 image 原图,由相应的算法来产生缩略图。 } private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException { inputStream.defaultReadObject(); generateThumbnail(); } }
在上面的例子中, thumbnailImage 是通过调用 generateThumbnail 方法产生的一张缩略图。
thumbnailImage 字段被标记为 transient ,所以只有原始图片 image 字段被序列化了,缩略图 thumbnailImage 不会被序列化。这意味着保存被序列化后的对象时需要更少的存储空间。
在反序列化时,readObject 方法会被调用,为了使对象的状态恢复到被序列化时,readObject 方法会执行所有必要的操作。在这里, thumbnail 缩略图需要被生成,所以 readObject 被重写了,所以 thumbnail 缩略图可以通过调用 generateThumbnail 方法来生成。
想要了解更多的信息,可以参考 Discover the secrets of the Java Serialization API 这篇文章。
- java 中 transient 关键字意义
- java中transient关键字
- java中transient关键字
- java中transient关键字简介
- java中transient关键字作用
- Java中Transient关键字解释
- Java 中transient 的关键字用法
- java中transient、volatile、strictfp关键字说明
- JAVA中transient关键字的使用
- java中transient关键字的作用
- Java中transient关键字的应用
- java中transient关键字学习记录
- java中transient关键字学习记录
- 浅谈Java中transient关键字的作用
- java中transient关键字使用学习
- java中关于transient关键字小结
- java关键字Transient
- java关键字Transient
- 又见01背包 01背包 小技巧
- 面试题04
- HashMap的数据结构
- 简单工厂模式
- Linux学习-系统信息查看命令小结
- java 中 transient 关键字意义
- [教程]手把手教你制作高通虚拟按钮
- oc之常用类(一)字符串,数组,NSNumber
- linux下安装sublime_text (linux(centos ubuntu) install sublime_text)
- 百度地图开发关于导入最新的3.0以上jar包出现Unable to execute dex: Multiple dex files define Lcom/baidu/a/a/a/a; 错误的解决方案
- 再探正则表达式与通配
- 5Y70也并非美如画,德国网站对5Y70与5Y10的对比评测
- poj1731 Orders dfs
- 面向对象(继承-概述2)