Member-only story

Understanding the Transient Keyword in Java: A Deep Dive

Naveen Metta
4 min readFeb 1, 2025

--

credit goes to the owner : https://www.edureka.co/blog/transient-keyword-in-java/
source: edureka.co

As a Java developer, you’ve probably encountered the transient keyword, but do you really understand its power and when to use it? In this article, we'll explore this often-misunderstood keyword and see how it compares to other Java modifiers. We'll use simple examples to show you exactly when and why you should use transient in your code.

What is the Transient Keyword?

Let’s start with the basics. The transient keyword in Java tells the Java Virtual Machine (JVM) to skip a field during serialization. Think of it as telling Java, "Hey, don't save this field when you're saving the object!"

Here’s a simple example:

class User implements Serializable {
private String name; // Will be serialized
private transient String password; // Won't be serialized

public User(String name, String password) {
this.name = name;
this.password = password;
}
}

When you save (serialize) this User object, the password field won’t be included. This is perfect for sensitive data that you don’t want to persist.

Transient vs Static: Understanding the Difference

--

--

Naveen Metta
Naveen Metta

Written by Naveen Metta

I'm a Full Stack Developer with 3+ years of experience. feel free to reach out for any help : mettanaveen701@gmail.com

No responses yet