In this example, the thumbnailImage is a thumbnail image that is generated by invoking the generateThumbnail method.

The thumbnailImage field is marked as transient, so only the original image is serialized rather than persisting both the original image and the thumbnail image. This means that less storage would be needed to save the serialized object. (Of course, this may or may not be desirable depending on the requirements of the system -- this is just an example.)

At the time of deserialization, the readObject method is called to perform any operations necessary to restore the state of the object back to the state at which the serialization occurred. Here, the thumbnail needs to be generated, so the readObject method is overridden so that the thumbnail will be generated by calling the generateThumbnail method.

For additional information, the Discover the secrets of the Java Serialization API article (which was originally available on the Sun Developer Network) has a section which discusses the use of and presents a scenario where the transient keyword is used to prevent serialization of certain fields.

But why is it a keyword, and not an annotation @DoNotSerialize?
–
Elazar LeibovichMar 26 '11 at 21:46

145

I guess, this is owned to a time when there were no annotations in Java.
–
Peter WippermannApr 4 '11 at 13:48

17

I find it odd that serializable is internal to Java. It can be implemented as an interface or abstract class that requires users to override the read and write methods.
–
calebFeb 17 '12 at 19:30

5

@MJafar: readObject is usually chained into deserialization mechanisms and thus called automatically. Furthermore, in many cases you do not need to override it - the default implementation does the trick.
–
Mike AdlerAug 8 '13 at 7:36

3

@caleb probably because dealing with binary formats yourself is incredibly painful in Java due to the lack of unsigned integers.
–
rightfoldJan 18 '14 at 22:19

Before understanding the transient keyword, one has to understand the concept of serialization. If the reader knows about serialization, please skip the first point.

What is serialization?

Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes and stored in a file. In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the Serialization interface. It is a marker interface without any methods.

Now what is the transient keyword and its purpose?

By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as transient. If the variable is declared as transient, then it will not be persisted. That is the main purpose of the transient keyword.

This part strikes me as odd and possibly confusing: "That means the state of the object is converted into a stream of bytes and stored in a file". It seems to me that most of the time serialization does not involve writing to a file (case in point: the networking examples that follow)
–
Garcia HurtadoNov 24 '14 at 2:11

In an object you may have information that you don't want to serialise/persist (perhaps a reference to a parent factory object), or perhaps it doesn't make sense to serialise. Marking these as 'transient' means the serialisation mechanism will ignore these fields.

One example of when this might be useful that comes to mind are variables that make only sense in the context of a specific object instance and which become invalid once you have serialized and deserialized the object. In that case it is useful to have those variables become null instead so that you can re-initialize them with useful data when needed.

What is transient variable in Java?
In simple sentence any variable which is modified with transient keyword becomes transient variable in java.

Why do we need transient variable in java?
Transient keyword provides you some control over serialization process and gives you flexibility to exclude some of object properties from serialization process. Some time it does make sense not to serialize certain attributes of an object, we will see which variables should not be serialized and should be made transient in next section.

Which variable you should mark transient?
Since we know the purpose of transient keyword or having transient variable its make sense to think about which variable should be marked as transient. My rule is that any variable whose value can be calculated from other variables doesn't require to be saved. For example if you have a field called "interest" whose value can be derived from other fields e.g. principle, rate, time etc then there is no need to serialize it.
Another example is of word count, if you are saving article then no need to save word count, because it can be created when article gets deserialized. Another good example of transient keyword is "Logger" since most of the time you have logger instance for logging in Java but you certainly don't want it to serialize correct?

transient is used to indicate that a class field doesn't need to be serialized.
Probably the best example is a Thread field. There's usually no reason to serialize a Thread, as its state is very 'flow specific'.

Correct me if I'm wrong, but Thread is not serializable so it will be skipped anyways?
–
TFennisJul 23 '13 at 10:54

1

@TFennis: If a serializable class A references a not serializable class B (like Thread in your example), then A must either mark the reference as transient XOR must override the default serialization process in order to do something reasonable with B XOR assume that only serializable subclasses of B are actually referenced (so the actual subclass must take care for their "bad" parent B) XOR accept that the serialization will fail. In only one case (marked as transient) B is automatically and silently skipped.
–
A.H.Nov 26 '13 at 22:45

Serialization systems other than the native java one can also use this modifier. Hibernate, for instance, will not persist fields marked with either @Transient or the transient modifier. Terracotta as well respects this modifier.

I believe the figurative meaning of the modifier is "this field is for in-memory use only. don't persist or move it outside of this particular VM in any way. Its non-portable". i.e. you can't rely on its value in another VM memory space. Much like volatile means you can't rely on certain memory and thread semantics.

as per google
transient meaning == lasting only for a short time; impermanent.

now if want to make anything transient in java use transient keyword.

Q: where to use transient?

A: Generally in java we can save data to files by acquiring them in variables and writing those variables to files, this process is known as Serialization. Now if we want to avoid variable data to be written to file, we would make that variable as transient.

There are use cases other than sensitive data in which you may not want to serialize a field. For example, you probably would never want to serialize a Thread (credit to @A.H. for example), in which case you would mark it as transient. However, a thread is not sensitive data in and of itself, it just makes no logical sense to serialize it (and it is not serializable).
–
glen3bMay 9 '14 at 18:24

@glen3b That case isn't excluded by this answer. It certainly is needed as things stand in the case the poster mentioned.
–
EJPJul 1 at 13:32

Before I respond to this question, I must explain to you the SERIALIZATION, because if you understand what it means serialization in science computer you can easily understand this keyword.

Serialization
When an object is transferred through the network / saved on physical media(file,...), the object must be "serialized". Serialization converts byte status object series. These bytes are sent on the network/saved and the object is re-created from these bytes.Example