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.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
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.
Classes are loaded when they are instantiated. For Visualforce page controllers, this is when the page is loaded, for other classes, this is when the class is instantiated via the code.
When this line is executed the Myclass class is loaded, and the constructor is executed. The instance is destroyed when the transaction ends, and a new instance is created the next time the code is executed.
According to that definition: Loading = when the class is being used
That means it loads when you try to access the class.
So, you have a controller, it will 'load' the static variables in the controller when the page calls it.
As other answers talk about, static variables in Apex are short lived compared with other environments and are not preserved between requests.
But they are useful for caching within an individual request. For example, if you have code in a controller that requires a record type id and code in a trigger that requires the same record type id, then "lazily loading" once into a static variable can make sense (and make your code cleaner too). This particularly benefits larger code bases where the cached data gets referenced many times per request.
But caching too much would be a mistake because every request would carry the overhead of the cache filling work whether the cache content was used or not.
Dan Appleman's Advanced Apex Programming book covers the topic of how static works well. I recommend anyone working in Apex to read it (and no I am not on commission).