function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
iRiteshiRitesh 

How apex get executed in Salesforce

i write a Batch apex.i write an execute method .which is using a static variable of another class.because it is shhared.any one can use it and change it.

 

public void execute(Database.BatchableContext BC, List<sObject> scope){

     x=GMirror.x;

     ...........

     ...........

         .........

y =GMirror.x; 


}

 

i want to know each batch of 5 in my case will run in transaction or means at this time only this method is running nothing else or in other words i want to know if using another class's method execution  in between time first statement execute

     x=GMirror.x;

 

and second statement 

y =GMirror.x; 

 

its possible by another parallel execution of another class's method that  these value of GMirror.x get changed ??

 these execution  this necessary that no one changed value of GMirror.x or value of GMirror.x can be changed by another parallel execution.Please clarify .

bob_buzzardbob_buzzard

No, it is not possible for another transaction to modify a static variable that is in use in your transaction.

 

Static variables in apex are different to other programming languages (e.g. Java), due to the multi-tenant nature of the platform.  In something like Java, when the JVM is created, the static variables are instantiated and are shared across the entire JVM.  This wouldn't work in Salesforce, as there is effectively a single virtual machine for use by all clients.  Static variables are instantiated when you commence a transaction and are removed once the transaction completes - they do no persist across transactions.

 

This is covered by a one-liner in the Apex Developer's Guide:

 

--- snip ---

 

Static variables are only static within the scope of the request. They’re not static across the server, or across the entire organization. 
 
--- snip ---