Search code examples
salesforceapex

What are the use cases of using the Static variable and Instance variables in apex?


I have read from various different sites about the static and instance variables but I am not able to understand when to use Static and when instance in the class. I need to know the use cases of both type of variables according to the real world requirements.


Solution

  • To store information that is shared across instances of a class, use a static variable.

    Instance variables are used by an instance of a class, i.e., by an object. An instance member variable is declared inside a class, but not within a method

    Check the link for examples : https://gvallejos.com/static-and-instance-in-apex-salesforce/