Great Answers to
Questions About Everything


I am actually adding a static field to one of the classes. The field would hold a small cache. The documentation says that the static fields are initialized when the classes are loaded. But, when are the classes actually loaded? Are they loaded when they are first created? Are they reloaded each time I modify it? Or does reload whenever any class or object in the Salesforce Org changes.

{ asked by user593644 }


One fundamental principle of apex to remember is that there are no long running processes. As opposed to something like an app running in tomcat where your static variables are shared between multiple requests apex threads are totally isolated and share nothing outside of the database with each other.

Thus in two requests that happen simultaneously from the same user it's possible to have two different values for the same static variable, since each thread is totally independent from each other. When the thread completes the action is was started to perform (a single API request, a visualforce page action, etc.) it's state is discarded and the thread terminates. This is why the other answers are valid technically - the class is loaded once per transaction and then is unloaded when the transaction completes. Modifying an apex class doesn't load it because there's no running apex thread that needs to reference it at that point.

With that in mind static blocks and initializers in apex are run once the class is first referenced in your code. Running the constructor, referencing a static property, method, or any other reference to the class causes the system to load the class at that point.

{ answered by ca_peterson }