function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

What are the static variables in salesforce..?

Can Body help me What are the static variables in salesforce..? give me some examples..




Please refer below URL:-


If you still have any question, please let me know.


Happy to help you!


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



If you found this answer helpful to you... Please accept this as a Solution and  give kudos by clicking on the star icon.


Thanks and Regards,

Arunkumar.R | Salesforce Certified Developer


Hi ,

Static methods, variables, or initialization code are associated with a class, and are only allowed in outer classes.

When you declare a method or variable as static, it's initialized only once when a class is loaded. All static member variables in a class are initialized before any object of the class is created.  Indeed they aren't transmitted as part of the view state for a Visualforce page.
Using static variables will cause only one instance of the variable to be loaded when the application loads and that variable will not go out of memory until the app is closed.  It holds information that is common to all instances on a class and It is shared between them instead of being created a new with each instance.

Read here for more details.

Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You .

Static variables are those which  can be used without  instantiating  a class.Instance variables are called by object while static are called by class.Static variables and methods are not included in view state for a vf page. 
farukh sk hdfarukh sk hd
Static variable are global variable which are not related to particular object,You can call a static variable directly by class name without requiring instance of class similar goes for static methods.

For static variable and static method concept in detail visit,
A static variable defined in a trigger doesn't retain its value between different trigger contexts within the same transaction, such as between before insert and after insert invocations. Instead, define the static variables in a class so that the trigger can access these class member variables and check their static values.
A class static variable can’t be accessed through an instance of that class. If class MyClass has a static variable myStaticVariable, and myClassInstance is an instance of MyClass, myClassInstance.myStaticVariable isn’t a legal expression.

The same is true for instance methods. If myStaticMethod() is a static method, myClassInstance.myStaticMethod() isn’t legal. Instead, refer to those static identifiers using the class: MyClass.myStaticVariable and MyClass.myStaticMethod().

if it is useful to
Please Mark as best ans.
Ackerman AotAckerman Aot
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.
Hope that's help.
syed jabeenasyed jabeena
Hi, Ganeeesh

Using Static Methods and Variables

You can use static methods and variables only with outer classes. Inner classes have no static methods or variables. A static method or variable doesn’t require an instance of the class in order to run.

Before an object of a class is created, all static member variables in a class are initialized, and all static initialization code blocks are executed. These items are handled in the order in which they appear in the class.

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 is static only within the scope of the Apex transaction. It’s not static across the server or the entire organization. The value of a static variable persists within the context of a single transaction and is reset across transaction boundaries. For example, if an Apex DML request causes a trigger to fire multiple times, the static variables persist across these trigger invocations.

To store information that is shared across instances of a class, use a static variable. All instances of the same class share a single copy of the static variable. For example, all triggers that a single transaction spawns can communicate with each other by viewing and updating static variables in a related class. A recursive trigger can use the value of a class variable to determine when to exit the recursion.

some interesting behavior in our test classes when using static variables to make sure triggers only fire once. Consider the following trigger, class, and testclass:


trigger RecursiveTrigger on Account (before insert) {
                       if(RecursiveClass.RunOnce) {
                            RecursiveClass.RunOnce = false;
                            if(Trigger.isInsert) {
                                if(Trigger.isUpdate) {

  public class RecursiveClass {
          public static boolean RunOnce = true;
             public static void doStuffOnInsert() {}
             public static void doStuffOnUpdate() {}



public class TestRecursiveClass {

          static testMethod void testAccountInsertUpdate() {
                  Account a = new Account(Name = 'Testing Recursive'); 
                   insert a;
                    a.Name = 'Testing Update';
                     update a;

  If you found this answer helpful to you... Please Mark it as a best Answer...

Syed Jabeena