Skip to main content
edited tags
Link
crmprogdev
  • 41k
  • 9
  • 58
  • 116
Source Link
Sukruti
  • 472
  • 8
  • 25

Difference between Transient and Static variable?

In SF docs, we will get an example of transient variable getting recreated each time the action is done via button. It will help to reduce view state. Although if I used static keyword the view state will get reduced. What is the actual difference and advantage of both keyword on apex controller.

<apex:page controller="ExampleController">
Time1: {!t1} <br/><br/>
Time-Transient: {!t2} <br/><br/>
<apex:form >
<apex:commandLink value="Refresh"/>
</apex:form>
</apex:page>

public class ExampleController {

    DateTime t1;
    static DateTime t2;

    public String getT1() {
        if (t1 == null) t1 = System.now();
        return '' + t1;
    }

    public String getT2() {
        if (t2 == null) t2 = System.now();
        return '' + t2;
    }
}