java transient keyword

In Java transient is a modifier and it can be applied to variables/properties.when any variable is declared with transient then that variable value will not be persist when object is stored.
At The Time of Serialization JVM Will ignores the original value of transient variable.and JVM will save default value to the Files.
transient variable cannot be serialized.
Example to java transient keyword:
package com.java2study;
public class TransientDemo implements Serializable{
private static final long serialVersionUID = 1L;
private int userId;
private transient String password;

public int getUserId() {
return userId;

public void setUserId(int userId) {
this.userId = userId;

public String getPassword() {
return password;

public void setPassword(String password) {
this.password = password;

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
TransientDemo t = new TransientDemo();
FileOutputStream fos = new FileOutputStream("abcd.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);



In Above Example userid will save as number and password value will save as "null".
because we have declared password with JVM Will ignores the original value and it saves the
default values. for string default is "null"

No comments:

Post a Comment