Java's transient keyword
- User
Java's transient keyword
- Assistant
In Java, the
transientkeyword is used to indicate that a variable should not be serialized when an object is converted into a byte stream.When an object is serialized, all of its instance variables are converted into a sequence of bytes that can be written to a file or sent over a network. However, there are certain cases where it is not desirable to serialize certain variables.
For example, let's say you have a class
Personwith instance variablesname,age, andpassword. You might want to persist thenameandagevariables, but not thepasswordvariable. In this case, you can declare thepasswordvariable astransientlike this:public class Person implements Serializable { private String name; private int age; private transient String password; // constructor, getters and setters }