How does Static variable and Static method works

  • I read in salesforce documentation -

    Static variables are only static within the scope of the request. They’re not static across the server, or across the entire organization.

    Can any one explain what does it mean? If i take a scenario like below -

    I have a class XX which has one static boolean variable state. Now i create 3 instances of class XX say A, B, C and set state = True in instance A.

    Now what would happen if i see state value in object B and C. Will it be True OR False.

    If True then what Static variables are only static within the scope of the request means?


    Thanks for such a intelligent answers. May you please answer below question as well?

    Suppose i have class like below -

    public with sharing class GlobalVariables 
        public static Boolean fireValidation = true;

    I have after insert, before update trigger on object where i check following condition -

    if(GlobalVariables.fireValidation) {
        // Fire some validations.

    now there are two different code flows.

    1) In first code flow like below i set this Global_Variables.PCFlag = false and update the record. will validations get fired? // As per my understading so far NO as fireValidation is set to false.

        if (some condition) {                                  
          GlobalVariables.fireValidation = false;
          // Update record.
          GlobalVariables.fireValidation = true;

    2) In other code flow like below i don't set Global_Variables.PCFlag = false and save the record. will validations get fired? // I think validations will get fired as it will run in other context.

        if (some condition) {                                  
         // Update record.

    3) What will happen if at the same time user 1 is executing this trigger as per condition1, user 2 is also executing the same trigger? Do they overwrite each other's value? // I think they wouldn't as they will run in their own context.

    static variables and methods are attributes only to the class and not to any instance of the class

  • Tech Matrix

    Tech Matrix Correct answer

    8 years ago

    Understanding the execution context is very important to understand how 'static' works in apex. As Mohith pointed out static doesn't behave the way as it works in java/c#. An execution context is one which either initiated by a user (e.g. through visualforce page action or apex trigger) or could run on own (e.g. scheduler) and executes a sequence of events/code. A static variable retains the value only during this execution context.

    To answer your question, the value of the variable 'state' will be retained and visible across all three classes named 'A', 'B' and 'C' only when these classes are instantiated in the same execution context. Let's say two different users access the same visual force page and perform an action - then we have two execution contexts here and the value of this variable 'state' will be different between the two different execution contexts. This is in contrast to java/c#, where once the static variable is initialized, the value of the static variable is same to all users. This link may help you to understand the execution context.

    I really hate the "it doesn't work the way it works in Java/C#", because it does actually work the same way. If you run the same Java program five times, *each program has its own copy of the static variable*. They can't magically communicate with each other through this static variable. In that sense, static is right what it says on the tin: static within the current execution context.

    Yes, but in say a .Net web app a static variable would have the same value for different users if they're served by the same process. It is a flawed comparison, though, because even then, with web farms, different processes could have different values.

    In .NET if you have a static variable in ASP.NET code behind file and if multiple users are accessing the same page, then this static variable is shared. This is not the same behavior in apex and that is what I and Mohith are trying to point out.

    The comment is still misleading. I've programmed in Java off and on for over a decade, and I've never had a "shared static" variable, nor would I expect my static variable to magically share itself across all instances of my application.

    Dug into that a bit more. Tomcat "re-uses" classes, so static variables can span a session. But, if one wrote a generic Java HTTP server that spawned a process for each connection, then the static variables would be isolated. Hence, the confusion. Java itself isn't the problem, it's the applied usage of Java in a specific context.

    You are right. To set the context right, the static variable would not retain the value in both java/c# if they are run as independent programs and the execution context are not shared. But if they are run in a application server environment where the application is shared among the users, then the variable is shared among the users.

    I come from a history of CGI-style web development, and Java desktop development, and so it would seem unnatural to assume that a static variable would span transactions. I suppose I need to play with Tomcat more. The original statement is sure to cause confusion, though. I suppose they figure that there's not that many "desktop" developers anymore?

    I have added few more conditions in my question. It would be great if you can answer those as well.

    Static variables inside are shared in the memory space of the w3svc.exe process and are NOT thread-safe. IIS creates an AppDomain in which your assemblies are loaded and w3svc.exe handles this. The static variables/methods can be accessed and modified by any user of the application. Unless you write your own lock mechanism around the storage of those values there will be strange issues:) This is why HttpApplication Session is used.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used