Quantcast
Channel: Difference between Transient and Static variable? - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 4

Difference between Transient and Static variable?

$
0
0

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;
    }
}

Viewing all articles
Browse latest Browse all 4


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>