Static and Instance in Apex Salesforce

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.

Static : Static methods, variables and initialization code have following characteristics.

  • They are associated with a class.
  • They’r allowed only in outer class.
  • They’r initialized only when the class is loaded.
  • They’r not transmitted as part of the view state for a VF page.

Instance(Local) : Instance methods, member variables, and initialization code have these characteristics.

  • They’r are associated with a particular object.
  • They’r created with every object instantiated from the class in which they’r declared.

Local Variables have following characteristics –

  • They’re associated with the block of code in which they’re declared
  • They must be initialized before they’re used.

Static Methods and Variables – 

Static methods ans variables used only with outer class. Inner class have no static methods or variables. A static method or variable doesn’t require an instance of the class to access, you can access directly using the class name.

A static variable is static only within the scope of the apex transaction, it’s not static across the server or organization. Ex – If an apex DML cause a trigger to fire multiple time, the static variable persist across these trigger call. [Use static variable to avoid trigger recursive]

Static variable is used to store information that is shared across all instances of a class. Ex- A recursive trigger can use the value of a class variable to determine when to exit the recursion.

 public class runOnce {  
     public static boolean firstRun = true;  
 trigger AccTrigger on Account (before delete, after delete, after undelete) {  
     if(Trigger.isBefore && Trigger.isDelete){  
             Trigger.old[0].addError('Before Account Delete Error');  

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.

 public static void method() {  
     String Database = '';  
     Database.insert(new Account());  

Local variable names are evaluated before class names. If a local variable has the same name as a class, the local variable hides methods and variables on the class of the same name. As in the above example you have local variable and class having same name- Database, it will give compile time error, because Salesforce reports that the method doesn’t exist or has an incorrect signature.

Instance Methods and Variables –

An instance member variable is declared inside a class, but not within a method. 

Initialization Code :-

{                                                                              static {
//Code Here                                                              // Code Here
}                                                                               }

Initialization code is a block of code as above and static initialization code is a block of code preceded with keyword static.

The instance initialization code in a class is executed each time an object is instantiated from that class. These code blocks run before the constructor. A static initialization block runs only once, regardless of how many times you access the class that contains it.

A class can have any number of either static or instance initialization code blocks. The code blocks are executed in the order in which they appear in the file.

4 thoughts on “Static and Instance in Apex Salesforce

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s