What does the keyword “transient” mean in Java? [duplicate]

喜夏-厌秋 提交于 2019-11-26 10:19:55

问题


This question already has an answer here:

  • Why does Java have transient fields? 13 answers

I saw somewhere


transient private TrackDAO trackDAO;


回答1:


Google is your friend - first hit - also you might first have a look at what serialization is.

It marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost intentionally.

Example from there, slightly modified (thanks @pgras):

public class Foo implements Serializable
 {
   private String saveMe;
   private transient String dontSaveMe;
   private transient String password;
   //...
 }



回答2:


Transient variables in Java are never serialized.




回答3:


It means that trackDAO should not be serialized.



来源:https://stackoverflow.com/questions/5245600/what-does-the-keyword-transient-mean-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!