当前位置: 技术问答>java相关
谁能讲一个transient的用法,或者分析一下下这段话?
来源: 互联网 发布时间:2015-05-09
本文导语: transient • Transient modifier should not be specified for static variables as these do not belong to objects • Transient variable can be both static and final • The class need not be Serializable or Externalizable to declare its f...
transient
• Transient modifier should not be specified for static variables as these do not belong to objects
• Transient variable can be both static and final
• The class need not be Serializable or Externalizable to declare its fields as "transient" [ though its meaningless
to do so ]
• But to write the instance of the class ( serialize) into a stream, the class need to implement Serializable or
Externalizable, else "NotSerializableException" will be thrown.
• Objects can be stored using serialization i.e. they can be later retrieved in the same state as when they were
serialized. these are persistent objects.
• Transient indicates that the variable need not be saved when the object is put in persistent storage.
class x implements Serializable{ //Serializable saves the value of the non-transient instances
transient int Temperature;
}
• Transient modifier should not be specified for static variables as these do not belong to objects
• Transient variable can be both static and final
• The class need not be Serializable or Externalizable to declare its fields as "transient" [ though its meaningless
to do so ]
• But to write the instance of the class ( serialize) into a stream, the class need to implement Serializable or
Externalizable, else "NotSerializableException" will be thrown.
• Objects can be stored using serialization i.e. they can be later retrieved in the same state as when they were
serialized. these are persistent objects.
• Transient indicates that the variable need not be saved when the object is put in persistent storage.
class x implements Serializable{ //Serializable saves the value of the non-transient instances
transient int Temperature;
}
|
你一定是要考SCJP吧,只能用于变量,考试的时候是不会出的,呵呵。
|
一时很难说清楚
建议你看一下中文版(别说你没有)的“对象序列化”那部分,上面有用法,也比较容易理解。
建议你看一下中文版(别说你没有)的“对象序列化”那部分,上面有用法,也比较容易理解。
|
transient修饰的变量表示该变量只存在于内存
序列化保存到硬盘的时候不保存该变量
比如密码什么的不想保存到硬盘,就加个transient
序列化保存到硬盘的时候不保存该变量
比如密码什么的不想保存到硬盘,就加个transient
|
如果一个变量是 Transient,序列化时就不被存储到序列中。
这样当再次从序列中读出该对象时,这个变量的值就是没有的。
基于这点,在编程时就要注意,如果你的对象可能被序列化,如序列化到磁盘文件或通过CORBA序列化的传送出去,那么在代码中就不应该默认该Transient变量总是有值的。 也就是要注意对这些变量的null的判断。
这样当再次从序列中读出该对象时,这个变量的值就是没有的。
基于这点,在编程时就要注意,如果你的对象可能被序列化,如序列化到磁盘文件或通过CORBA序列化的传送出去,那么在代码中就不应该默认该Transient变量总是有值的。 也就是要注意对这些变量的null的判断。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。