3

Suppose I have a batch job and I want to maintain state (i.e. want to keep a log of the record ids that I've processed, or keep a running total, or something).

When would I use class variables vs. static variables in my batch class?

After the batch job finishes, do I have access to those variables in the class anymore?

2 Answers 2

4

There's a maximum amount of data you can serialize before the batch will fail (and also counts against your heap limits during execution). Static variables serve the same purpose as transient variables in Visualforce; they will not be serialized and therefore not count against heap. On the downside, static variables are reset between each transaction, so they will be null or set to their default initialization value, if any.

In general, if you need to track the data, make sure it is not static, and if you do not need to track the data, make sure it is static. The more data you have to serialize, the slower your batch will run (serialization/deserialization requires more CPU time).

The static variables will be available in unit tests after the batch execution, but the instance variables will not. None of these variables will be observable after the finish method returns in normal execution (e.g. execute anonymous scripts or Visualforce page contexts). If you need access to the results, you need to persist the data somewhere, such as the Platform Cache, a database record, etc.

Static variables generally have two purposes: unit tests and sharing data across methods. The unit test bit is obvious; if you want to see what happened during the execution, you need a place to store it. The static variable can be accessed by the unit test after execution. As far as sharing data between methods, some developers prefer this method, but should probably be used sparingly, since it makes it less obvious which bits of data belong to which method.


Edit:

Alternatively, if I have a batch job that doesn't maintain state (doesn't use Database.Stateful ) how do member and static variables behave?

In this mode, static variables work as before (they are not serialized and reset to null/default value), while instance variables are reloaded from the batch's initial state when it was placed in the queue using Database.executeBatch. No changes to instance variables are preserved between start, each execute, and finish calls.

1
  • 1
    @user11235813 Edited answer to the question. In summary, the only difference is that changes to instance variables are instead lost and reset back to the moment database.executebatch was called.
    – sfdcfox
    Commented Jan 18, 2019 at 21:22
4

Per documentation (emphasis mine):

If you specify Database.Stateful in the class definition, you can maintain state across these transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t retain their values and are reset between transactions.

If you are looking to maintain state, then you will need to implement Database.Stateful and that utilize an instance variable to calculate any value.

Because these are instance variables, you will have access to these variables during the life cycle of the class, e.g., you will be able to access these variables in finish method as well.


Class Variables (are same as Static Variables) can be used to share a value between different instances of the same class. You can find more on Static variable on its documentation here.

To store information that is shared across instances of a class, use a static variable. All instances of the same class share a single copy of the static variable.

2
  • so, when would I want to use a static variable in a stateful batch class? Commented Jan 18, 2019 at 20:45
  • You will normally use a static variable when you want to share the value of say a variable across different instances of the same class. I have added some references around this in my answer.
    – Jayant Das
    Commented Jan 18, 2019 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .