You need to sign in to do that
Don't have an account?
Kaity
Static keyword
Hi,
when do you use a static variable in Apex? for exam:
global with sharing class AccountRemoter{
public String accountName { get; set; }
public static Account account { get; set; }
-Kaity
The perfect example of this will be at the time of recursive trigger call.
When a trigger is calling recursively then in the trigger you can use a static variable to check whether it is true or not. Then execute the trigger only basis on not true. For e.g
Static variable contains in this class
public class FutureTriggerController{
public static boolean isFutureUpdate = false;
}
Than in the trigger check like this
trigger TriggerName on Account(after insert){
if(FutureTriggerController.isFutureUpdate != true){
FutureTriggerController.isFutureUpdate = true;
//Your trigger code
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
Go through this link in details. You will find your answer
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm
Thanks
1.static key word can be used with cariables and methods but not with classes.
2.Static memebrs or blocks loads before class loaded.
3.Static members main purpose is 'sharing a copy of value by all the instances'.
4.static methods we use in test classes because you can directly access static methods.
ex;
class Test{
public static a=10;
public static void method1(){
}
//to access first two elements out side the controller or class we doesn't require to create object
public void method2(){
}
}
My confusion is not in static method. But is in static variable. Why do we set a variable static and when do we use that.
Sorry for the confusion.
- Kaity
The perfect example of this will be at the time of recursive trigger call.
When a trigger is calling recursively then in the trigger you can use a static variable to check whether it is true or not. Then execute the trigger only basis on not true. For e.g
Static variable contains in this class
public class FutureTriggerController{
public static boolean isFutureUpdate = false;
}
Than in the trigger check like this
trigger TriggerName on Account(after insert){
if(FutureTriggerController.isFutureUpdate != true){
FutureTriggerController.isFutureUpdate = true;
//Your trigger code
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Thanks Souvik. Really nice.
Souvik-
Just to confirm.
This static variable flows once throughly, and then at end of the execution it loses its value.
Static variable as used to sttop the recursive state.
Please clarify?
Yes it will hold the value for a particular request and flows thorughout the triggers with that value. Then in the next request the value will be reset.
Thanks