+ Start a Discussion
KaityKaity 

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

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

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

souvik9086souvik9086

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

Naveen NelavelliNaveen Nelavelli

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(){

 

}

}

 

 

 

 

KaityKaity

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

souvik9086souvik9086

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

This was selected as the best answer
KaityKaity

Thanks Souvik. Really nice.

KaityKaity

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?

souvik9086souvik9086

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