Activity Forums Salesforce® Discussions What are some points to remember regarding Static Keyword in apex.

  • Anurag

    Member
    September 14, 2018 at 2:26 pm

    Hi Prachi,

    Static methods, variables, and initialization code have these characteristics.

    They’re associated with a class.
    They’re allowed only in outer classes.
    They’re initialized only when a class is loaded.
    They aren’t transmitted as part of the view state for a Visualforce page.

    In Apex, you can have static methods, variables, and initialization code. However, Apex classes can’t be static. You can also have instance methods, member variables, and initialization code, which have no modifier, and local variables.

  • Parul

    Member
    September 14, 2018 at 4:47 pm

    Hi

    1.static key word can be used with cariables and methods but not with classes.

    2.Static memebrs or blocks loads before class loaded.

    3.Static members main purpose is 'sharing a copy of value by all the instances'.

    4.static methods we use in test classes because you can directly access static methods.

     

     

    ex;

    class Test{

     

    public static a=10;

     

    public static void method1(){

    }

    Thanks

  • shariq

    Member
    September 14, 2018 at 9:40 pm

    Hi,

    To add more -

    A static method is used as a utility method, and it never depends on the value of an instance member variable. Because a static method is only associated with a class, it can't access the instance member variable values of its class. A static variable isstatic only within the scope of the Apex transaction.

    Hope this helps.

  • Avnish Yadav

    Member
    September 15, 2018 at 1:47 pm

    Hi,

    1.  Apex classes cannot be static.
      2. Static allowed only in outer class.
      3. Static variables not transferred as a part of View State.
      4.Static variables and static block runs in order in which they are written in class.
      5.Static variables are static only in scope of request.

    Thanks

Log In to reply.

Popular Salesforce Blogs