Transient Variable in Salesforce

Transient keyword to declare instance variable that can not be saved and should not transmitted as part of view state for visual force page. This helps us reducing the view state of visualforce page. As we all know that, there is a limit of 135KB of viewstate and many times this Transient keyword helps us to reduce the view state.

In below example I have created two Datetime variable, one is with transient keyword and another one is without transient keyword and populating value with both variables. I used Refresh button to refresh the Visualforce page, however only Datetime dt2Controller:

public class SampleController {
    DateTime dt1;
    transient DateTime dt2;
    public String getDT1() {
        if (dt1 == null) dt1 = System.Now();
        return '' + dt1;
    public String getDT2() {
        if (dt2 == null) dt2 = System.Now();
        return '' + dt2;

Visualforce Page:

<apex:page controller="SampleController">
    Datetime 1: {!dt1} <br/>
    Datetime 2: {!dt2} <br/>
    <apex:form >
        <apex:commandLink value="Refresh"/>

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.