Is there anyway to maintain the state of a certain variable in Batch Apex across transactions. Database.Stateful will save the state for all the declared variables, Can i set it for only one specific variable?


2 Answers 2


For the variables in stateful class in whom you don't wanna store state, you can make those variables as transient.

Use the transient keyword to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a Visualforce page. You can also use the transient keyword in Apex classes that are serializable, namely in controllers, controller extensions, or classes that implement the Batchable or Schedulable interface



You need to implement the Database.Stateful interface and declare an instance variable to keep its state. If you declare it as static, it doesn't maintain its state across batch jobs.


You must log in to answer this question.

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