"Hello, Amigo! I'd like to tell you one more little detail about serialization."

Suppose our class contains a reference to some InputStream. Then it can't be serialized, right?

"Right. You yourself said that streams can't be serialized. And you can't serialize an object that has non-serializable data."

"Right. Just so. But what if the class stores data that doesn't play a significant role in its state and yet prevents the class from being considered a serializable class? Never mind that a class might store unnecessary stuff. It could toss this data at any time and maybe it even does—all the time."

For these cases, Java's creators came up with the transient keyword. If we write this keyword before a member variable, then it will be ignored during serialization. Its state won't be saved or reconstructed. As if it didn't exist. This is just the thing for the situations we just considered.

Remember caching and the volatile modifier? There are no rules without exceptions.

Here's one such example of this happiness:

A «cat» example with a variable that is invisible to serialization:

class Cat implements Serializable
 public String name;
 public int age;
 public int weight;

 transient public InputStream in = System.in;