Activity › Forums › Salesforce® Discussions › What is the scope of static variable in Apex?
-
What is the scope of static variable in Apex?
Posted by Anurag algoworks on September 12, 2018 at 1:17 PMWhat is the scope of static variable in Apex?
Avnish Yadav replied 7 years, 9 months ago 5 Members · 4 Replies -
4 Replies
-
Hi,
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.
Hope this helps!
- [adinserter block='9']
-
Hi,
I think the scope for a Static variable in Apex is the entire transaction. Any static variables will maintain their value across that one transaction.
Thanks.
-
Hello Anurag,
A static variable defined in a trigger doesn’t retain its value between different trigger contexts within the same transaction, such as between before insert and after insert invocations. Instead, define the static variables in a class so that the trigger can access these class member variables and check their static values.
-
Hello Anurag,
Also on Async call, the static variable will not work.
Thanks.
Log In to reply.