function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SYED MOOSA NAZIR T NSYED MOOSA NAZIR T N 

"Static" and "Transient" variables concept with respect to VISUALFORCE Page

What is the difference between "Static" and "Transient" variables with respect to VISUALFORCE Page only.
Both are aren’t transmitted as part of the view state for a Visualforce page. Then what makes difference?

Below is the sample code I have written to understand this concept. But not understanding the exact difference.

VF Page Code
<apex:page controller="ViewStateVFCon" action="{!Method1}">
<apex:form >
<apex:pageBlock id="pb">
<apex:pageBlockTable value="{!StaticVar}" var="Tab1">
<apex:column headervalue="Record ID">
<apex:outputText value="{!Tab1.id}"/>
</apex:column>
<apex:column headervalue="Record Name">
<apex:outputText value="{!Tab1.Name}"/>
</apex:column>
</apex:pageBlockTable>


<br/><br/><br/><br/><br/><br/><br/><br/><br/>


<apex:pageBlockTable value="{!transeintVar}" var="Tab2">
<apex:column headervalue="Record ID">
<apex:outputText value="{!Tab2.id}"/>
</apex:column>
<apex:column headervalue="Record Name">
<apex:outputText value="{!Tab2.Name}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>

<apex:commandButton action="{!Method2}" title="Refresh" value="Refresh" reRender="pb"/>
</apex:form>

</apex:page>

VF Controller
public with sharing class ViewStateVFCon {
    public static List<Account> StaticVar {get;set;}
    public transient List<Account> transeintVar {get;set;}
    public void Method1 (){
        system.debug('Action Method1 from Page Action==    ');
        StaticVar = [select id, name from Account limit 20];
        transeintVar = [select id, name from Account limit 20];
    }
    public pageReference Method2 (){
        system.debug('Action Method2 from Command Button==    ');
        return null;
    }
}

 

Thanks Folks..
Syed Moosa Nazir TN
smartmoosa@gmail.com

AnupamAnupam
Hi Syed

Check this link : https://developer.salesforce.com/forums/?id=906F00000008mc6IAA
 
SYED MOOSA NAZIR T NSYED MOOSA NAZIR T N

Hi Anupam,

Hope you are doing great!! Thanks for sharing the link.

I have already gone through that link before posting.
In fact, when I searched in Google "static variable in view state salesforce". This was the first link appeared :) :) :)

I am not getting a clear picture (exact use case in Visualforce Page) from that link. 
When to use STATIC and When to use TRANSIENT in Controller to reduce ViEW STATE.

 

Thanks and regards
Syed Moosa Nazir TN

AnupamAnupam
Static is used when you want to access methods and variable without instantiating the object as they are local to where they are defined. Transient is for variable which you don't want to save and shouldn't be transmitted to server.

Mark it solved if it answers your question.