Find your content:

Search form

You are here

When are Apex classes loaded?

 
Share

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.


Attribution to: Amit

Possible Suggestion/Solution #1

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.

For example:

Myclass mClass = new Myclass();

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.


Attribution to: JimRae

Possible Suggestion/Solution #2

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.

public class MyController
{
  public static String TEMP = 'abc';

  public MyController()
  {
    //Your variable was loaded as soon as this controller got referenced by the Visualforce page
  }

  public PageReference someAction()
  {
    String otherVal = Util.OTHER_VAL;//The variables in your util class I believe are loaded here
  }
}

util class

public class Util
{
  public static String OTHER_VAL = 'something';
}

Attribution to: dphil

Possible Suggestion/Solution #3

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.


Attribution to: ca_peterson

Possible Suggestion/Solution #4

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).


Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31098

My Block Status

My Block Content