Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this article, we'll first understand the transient keyword, and then we'll see its behavior through examples.

2. Usage of transient

Let's first understand the serialization before moving to transient as it is used in the context of serialization.

Serialization is the process of converting an object into a byte stream, and deserialization is the opposite of it.

When we mark any variable as transient, then that variable is not serialized. Since transient fields aren't present in the serialized form of an object, the deserialization process would use the default values for such fields when creating an object out of the serialized form.

The transient keyword is useful in a few scenarios:

  • We can use it for derived fields
  • It is useful for fields that do not represent the state of the object
  • We use it for any non-serializable references

3. Example

To see it in action, let's first create a Book class whose object we would like to serialize:

public class Book implements Serializable {
    private static final long serialVersionUID = -2936687026040726549L;
    private String bookName;
    private transient String description;
    private transient int copies;
    
    // getters and setters
}

Here, we have marked description and copies as transient fields.

After creating the class, we'll create an object of this class:

Book book = new Book();
book.setBookName("Java Reference");
book.setDescription("will not be saved");
book.setCopies(25);

Now, we'll serialize the object into a file:

public static void serialize(Book book) throws Exception {
    FileOutputStream file = new FileOutputStream(fileName);
    ObjectOutputStream out = new ObjectOutputStream(file);
    out.writeObject(book);
    out.close();
    file.close();
}

Let's deserialize the object now from the file:

public static Book deserialize() throws Exception {
    FileInputStream file = new FileInputStream(fileName);
    ObjectInputStream in = new ObjectInputStream(file);
    Book book = (Book) in.readObject();
    in.close();
    file.close();
    return book;
}

Finally, we'll verify the values of the book object:

assertEquals("Java Reference", book.getBookName());
assertNull(book.getDescription());
assertEquals(0, book.getCopies());

Here we see that bookName has been properly persisted. On the other hand, the copies field has value 0 and the description is null – the default values for their respective data types – instead of the original values.

4. Behavior With final

Now, let's see a case where we'll use transient with the final keyword. For that, first, we'll add a final transient element in our Book class and then create an empty Book object:

public class Book implements Serializable {
    // existing fields    
    
    private final transient String bookCategory = "Fiction";

    // getters and setters
}
Book book = new Book();

The final modifier makes no difference – because the field is transient, no value is saved for that field. During deserialization, the new Book object gets the default value Fiction that’s defined in the Book class, but that value doesn’t come from the serialization data:

assertEquals("Fiction", book.getBookCategory());

5. Conclusion

In this article, we saw the usage of the transient keyword and its behavior in serialization and deserialization.

As always, all the code is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!