Hey friends,
Database.Stateful is a marker interface you add to a Batch Apex class so its instance member variables keep their values from one execute chunk to the next. Static variables still reset, and without Stateful every member variable goes back to its starting value each chunk.
That's the whole idea, and it's a favorite exam trap. Source: the Using State section of Use Batch Apex in the Apex Developer Guide. It sits under asynchronous Apex on the Platform Developer II exam.
Why do batch variables reset?
Each execute call is its own transaction. A job with 1,000 records and the default scope runs as five transactions of 200, and Salesforce rebuilds the class state for each. My Batch Apex guide covers that model in more detail.

Only instance variables, and only with Stateful.
What does Database.Stateful look like in code?
Here's a job that counts what it updated and reports the total in finish:
Remove Database.Stateful and finish prints 0, because recordsClosed resets to 0 before every chunk and again before finish.
What about values set in the constructor?
Without Stateful, a value you pass in the constructor is available in start, execute and finish, but only its initial value. Salesforce's example: if you change it in execute, the next chunk can't see the change. So constructor parameters are fine for configuration (a cutoff date, a query), not for running totals.
Cert Lab by Nick Frates
Let the Platform Developer II Cert Lab test you to success
Scenario questions that test exactly these traps: Stateful, static vs instance, scope sizes and async limits.
Start the Platform Developer II Cert Lab for $9.99 →Not ready to buy? Try a free lesson and see how fast you learn.
When should you use Database.Stateful?
- Counting or summarizing records as they're processed, like Salesforce's example of totaling opportunity amounts.
- Collecting failures (record IDs and messages) to email or log once in finish.
- Tracking progress you want to report at the end.
When shouldn't you use it?
Whatever you keep in state rides along to every later chunk, so keep it small. Don't hold every processed record in a list. Store counts and a capped list of error IDs instead. If you don't need anything carried across chunks, leave Stateful off.
What Database.Stateful mistakes come up most?
- Using a static variable for the counter. Statics reset even with Stateful.
- Forgetting the interface. Your counter silently reads 0 in finish.
- Storing whole record lists in state. It grows with every chunk.
- Thinking Stateful makes the job transactional. Each chunk still commits on its own.
Here's why I care about practice questions so much. I earned 17 Salesforce certifications, and Focus on Force was a big part of that. My early approach was study guide first, tests later. Flipping it, taking practice tests fast and often, cut my study time way down. Cert Lab is my version of that: fast testing with explanations that link to current Salesforce docs.
FAQ
Do static variables keep their value with Database.Stateful?
No. Only instance member variables keep their values. Static member variables reset between transactions.
Is Database.Stateful a method?
No. It's an interface with no methods. You add it to the implements list next to Database.Batchable.
Where is Database.Stateful documented?
In the Using State in Batch Apex section of the Apex Developer Guide page Use Batch Apex. It applies to classes that implement Database.Batchable.
What is a common Stateful use case?
Counting or totaling records across chunks and reporting the result in finish.
Where should you go next?
- QueryLocator vs Iterable for choosing what start returns.
- How to schedule Batch Apex so your total shows up every morning.
- Platform Developer II practice questions for more traps like this one.
Nick Frates, a senior Salesforce developer with 17 certifications, built the Platform Developer II Cert Lab at nickfrates.com: rapid-fire practice questions for every exam section, each answer explained.
Cert Lab by Nick Frates
Let the Platform Developer II Cert Lab test you to success
Lessons, flashcards, a quiz for each of the five exam sections and full practice exams, every answer explained.
Start the Platform Developer II Cert Lab for $9.99 →Not ready to buy? Try a free lesson and see how fast you learn.
Hope this helps!
Best,
Nick


