• #21397
    Salesforce | Shubham Shubham #231

    Difference between global and static keyword in Salesforce?

    Please describe with an example



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


    void staticTest(){
    static int i;
    void main(){





    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.

    Salesforce | shariq shariq #1

    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.


    Salesforce | Parul Parul #2

    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.
    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.
    public class OuterClass {
    // Associated with instance
    public static final Integer MY_INT;
    // Initialization code
    static {
    MY_INT = 10;


Viewing 4 posts - 1 through 4 (of 4 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?