What does the keyword "transient" mean in Java? [duplicate]
Asked Answered
K

3

192

I saw somewhere


transient private TrackDAO trackDAO;

Killarney answered 9/3, 2011 at 12:13 Comment(0)
B
276

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;
   //...
 }
Buber answered 9/3, 2011 at 12:14 Comment(0)
S
33

Transient variables in Java are never serialized.

Steck answered 9/3, 2011 at 12:18 Comment(0)
D
10

It means that trackDAO should not be serialized.

Dare answered 9/3, 2011 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.