transient Keyword

transient - keyword is used to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a Visualforce page (i.e. transient variables are not part of view state)

Example 1 :
    Transient Integer currentTotal;

Example 2 : uing datetime variables

  1. <apex:page controller="datetimecontroller" >
  2. T1 : {!t1} - non transient - part of VF page viewstate - viewstate holds its value for after refresh use <br/>
  3. T2 : {!t2} - transient - not part of VF page viewstate - gets new value from server for after refresh use <br/>
  4. <apex:form >
  5. <apex:commandLink value="refresh"/>
  6. </apex:form>
  7. </apex:page>




  1. public with sharing class datetimecontroller {
  2. datetime t1;
  3. transient datetime t2;
  4. public string getT2() {
  5.     if (t2==null)
  6.     t2=System.now();
  7.     return ' '+t2;
  8. }
  9. public String getT1() {
  10.     if(t1==null)
  11.     t1=System.now();
  12.     return ' '+t1;
  13. }
  14. }


Comments

Popular Posts