You need to sign in to do that
Don't have an account?
AnshulVerma
Static variables not acting as static
Hi,
As I see static variables (in Java, C/C++, C# etc.) static variables are values that remain persistent throughout the application, it can be read/set in all sessions and would access the same variable(the value would persist across the whole application). But, when I was working with the static members of Apex classes I found that their values do not persist and
Please refer the following code
//Static class
Class ABC
{
public static Integer myVal;
static
{
myVal = 0;
System.debug('static constructor is called');
}
}
Visualforce page
All
value = {!val}
Apex Page Controller
Class SampleCon
{
public PageReference test()
{
System.debug('----------------val='+ ABC.myval);
CommonHelper.myval = CommonHelper.myval + 2;
System.debug('----------------val='+ ABC.myval);
CommonHelper.myval = CommonHelper.myval + 2;
System.debug('----------------val='+ ABC.myval);
CommonHelper.myval = CommonHelper.myval + 2;
System.debug('----------------val='+ ABC.myval);
return null;
}
}
Now, what happens is that each time test is initiated ABC.myval comes as zero. As far as my experience with other technologies (.NET, Java etc.), i think that statics should persist their values.
Please suggest if there is something that i'm missing.
Thanks in advance,
Anshul Verma
As I see static variables (in Java, C/C++, C# etc.) static variables are values that remain persistent throughout the application, it can be read/set in all sessions and would access the same variable(the value would persist across the whole application). But, when I was working with the static members of Apex classes I found that their values do not persist and
Please refer the following code
//Static class
Class ABC
{
public static Integer myVal;
static
{
myVal = 0;
System.debug('static constructor is called');
}
}
Visualforce page
All
value = {!val}
Apex Page Controller
Class SampleCon
{
public PageReference test()
{
System.debug('----------------val='+ ABC.myval);
CommonHelper.myval = CommonHelper.myval + 2;
System.debug('----------------val='+ ABC.myval);
CommonHelper.myval = CommonHelper.myval + 2;
System.debug('----------------val='+ ABC.myval);
CommonHelper.myval = CommonHelper.myval + 2;
System.debug('----------------val='+ ABC.myval);
return null;
}
}
Now, what happens is that each time test is initiated ABC.myval comes as zero. As far as my experience with other technologies (.NET, Java etc.), i think that statics should persist their values.
Please suggest if there is something that i'm missing.
Thanks in advance,
Anshul Verma
is there any way if we can persist a value throughout one login session ?