What is a transient variable?
Transient variable can’t be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can’t be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.
Sample code for transient in case of Database
package com.javaskool;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "add_amount")
public class AddAmount implements java.io.Serializable {
@Column(name = "value1")
private Integer value1;
@Column(name = "value2")
private Integer value2;
@Transient
private Double total; //total will not get stored in above DB table
}
Sample code for transient in case of file handling.
package com.javaskool;
public class AddAmount implements java.io.Serializable {
private Integer value1;
private Integer value2;
private transient Double total; //total will not get stored in file
}
Recent Comments