+ Start a Discussion
Pulkit Sood 6Pulkit Sood 6 

Static variable NOT retaining its value, but Instance variable is

Static variable is not retaining its value

public class trial {
        Integer x=0;
        static Integer y=0;
    public void setX(Integer i){
    public Integer getX(){
        return x;
    public pageReference ChangeValue(){
        System.debug('action x - '+x+' to '+(++x)+' and y - '+y);
        return null;
    public void getChangeValue(){
        if(y==0){System.debug(this+' onclick y - '+y+' to '+(++y)+' & x - '+x+' to '+(++x));return;}
        System.debug(this+' onclick x - '+x+' to '+(++x)+' and y - '+y);

When page loads, onClick is run automatically, why?? but anyways it should set the value for static y=1, i.e. next time after "if" statements should run
it debugs as "y=0 to y=1"
then when i click the button it again debugs as "y=0 to y=1" i.e. the class isn't retaining Static variable's value, but would work/retain-its-value if i remove STATIC KEYWORD from defination. i.e. make it an instance variable instance
but, with static, it debugs as "y=0 to y=1" every time i click the button, though it should have been set as right after the first debug
<apex:page controller="trial">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"/>
    <apex:form >
              <apex:commandButton value="Change and Set Value" onClick="{!changeValue}" action="{!changeValue}"/>
    var x;
    function setValue(){
    var op=$("label[id$='oplbl']"); 
        x ='{!X}';
        alert('x - '+x);
        return x;

13:08:52:009 USER_DEBUG [20]|DEBUG|trial:[x=0, y=0] onclick y - 0 to 1 & x - 0 to 1

13:10:13:009 USER_DEBUG [14]|DEBUG|action x - 1 to 2 and y - 0
13:10:13:013 USER_DEBUG [20]|DEBUG|trial:[x=2, y=0] onclick y - 0 to 1 & x - 2 to 3   //second execution for onclick method must have the y's value=1, but it rather preserves x's value

13:14:17:009 USER_DEBUG [14]|DEBUG|action x - 3 to 4 and y - 0
13:14:17:013 USER_DEBUG [20]|DEBUG|trial:[x=4, y=0] onclick y - 0 to 1 & x - 4 to 5
Prakash NawalePrakash Nawale
Pulkit Sood,

A static variable is static only within the scope of the Apex transaction. It’s not static across the server or the entire organization. The value of a static variable persists within the context of a single transaction and is reset across transaction boundaries.

Fyi https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm;

Please mark this as best Answer.
Pulkit Sood 6Pulkit Sood 6
but howcome then instance variable retaining its value?? isn't it more "volatile" than static???
Prakash NawalePrakash Nawale
Hi Pulklt,

About the instnace variable serlized and stored in hidden form fields of visualforce page.

FYI : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

Please mark this as best answer if this helps.
Pulkit Sood 6Pulkit Sood 6
You a bot or a person???
David @ ConfigeroDavid @ Configero
If you are dealing with a Visualforce page/controller, you most likely do not need a static variable as the controller's state is serialized into the viewstate.

There are multiple levels of scope.  Let's assume scopes roughly similar to: organization, transaction, and instance.

Organization scope would be similar to sObject records, Custom Metadata, Custom Settings, or Platform Cache.  This data can persist for long periods of time

Transaction scope (such as your static variables) are initialized once for each transaction execution.  For example nesting insert/update calls in a single transaction will still have the same static variable assignments, but once that transaction completes you will lose those static variables since they are only valid for that single transaction scope.

Instance scopes generally are within a transaction, but in the context of a Visualforce page, the controller's state is serialized into a viewstate and passed back and forth to allow the Visualforce page to keep its state.
Pulkit Sood 12Pulkit Sood 12
@David, Thanks a lot man.. i'd put urs the best answer, but my that id is expired... you cleared up my doubt!!!