Sobes.tech
Junior

What does the keyword transient mean in Java?

sobes.tech AI

Answer from AI

The transient keyword applies to class fields and indicates that this field should not be serialized during standard Java serialization (using java.io.Serializable).

import java.io.Serializable;

public class User implements Serializable {
    private String username;
    private transient String password; // This field will not be serialized
    private int age;

    // Constructor, getters, and setters
    public User(String username, String password, int age) {
        this.username = username;
        this.password = password;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public int getAge() {
        return age;
    }
}

During deserialization, transient fields will be initialized with default values for their type (0 for numeric primitives, false for boolean, null for reference types).

What does the keyword transient mean in Java? — Java - sobes.tech