Activity Forums Salesforce® Discussions Difference between global and static keyword in Salesforce?

  • Radhakrishna

    Member
    August 2, 2017 at 6:13 am

    Hello Shubham,

    Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).

    Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be
    accessible only in this file (file scope).

    On the other hand global variables have to be defined globally, persists (life is) throughout the program, scope is also throughout the program. This means such variables can be accessed from any function, any file of the program.

    So if you have a global variable and u r distributing ur files as a library and you want others to not access your global variable, you may make it static by just prefixing keyword static (of course if same variable is not required in other files of yours).

    Example:

    void staticTest(){
    static int i;
    printf("%d\n",i);
    i++;
    }
    void main(){
    staticTest();
    staticTest();
    staticTest();
    }

    Output:

    0

    1

    2

    In above example use of local static variable is demonstrated.

    global static: you can declare static variable in global scope also it will be almost same as other global variable except it has initial value as zero, if you don't assign any value to it.

  • shariq

    Member
    August 2, 2017 at 12:41 pm

    Hi Shubham,

    Static Keyword :-

    Static variables are variables that belong to an overall class, not a particular object of a class. Think of a static variable to be like a global variable – its value is shared across your entire org. Any particular object’s properties or values are completely irrelevant when using static. Similarly, are methods that act globally and not in the context of a particular object of a class.

    Global Keyword :-

    This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the SOAP API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global.

     

  • Parul

    Member
    September 16, 2018 at 2:55 pm

    global: Defines a class, method, or variable that can be used by any Apex that has access to the class, not just the Apex in the same application.

    you can declare static variable in global scope also it will be almost same as other global variable except it has initial value as zero, if you don’t assign any value to it.
    Example:
    global class myClass {
    webService static void
    makeContact(String lastName) {
    // do some work
    }

     

    static: This keyword defines a method/variable that is only initialized once, and is associated with an (outer) class, and initialization code. We can call static variables/methods by class name directly. No need of creating instance of a class.
    Example:
    public class OuterClass {
    // Associated with instance
    public static final Integer MY_INT;
    // Initialization code
    static {
    MY_INT = 10;
    }
    }

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos