How Call Stack works in Salesforce in Hindi

Table of Contents

Instance Block OR Object Block in Salesforce

An instance block in Salesforce is also similar to Java, as it is a block of code that is executed each time an instance of the class is created. It is often used to initialize instance variables or to perform any other instance-specific initialization tasks.

Previous Sessions

If you missed the previous session here are the links

				
					public class MyClass {
   // Country => Currency Exchange Rate
   // India = USA
   // 1$ = 82Rupee
   // Country => State
   {
        "India" => {}
        "USA" => {}
        "UK" => {}
        System.debug('Instance block executed');
    }
    public MyClass(String country){
        System.debug('Constructor executed');
        "India" => States
    }
}
MyClass INDIAClass = new MyClass("India");
// Instance block executed
// Constructor executed
MyClass ukClass = new MyClass("UK");
// Instance block executed
// Constructor executed
				
			

Static Block OR Class Block in Salesforce

A static block in Salesforce is a block of code that is executed only once, when the class is loaded into memory, just like in Java. It is typically used to initialize static variables or to perform any other static initialization tasks.

Laptops.printInfo();

				
					public class MyClass {
    static {
        System.debug('Static block executed');
    }
}
				
			

Call Stack in Salesforce

In Salesforce, as in any other programming language, a call stack is a data structure that keeps track of the sequence of method calls during the execution of a program.

When a method is called, the program pushes a new frame onto the call stack to store information about the method’s arguments, local variables, and return address. When the method returns, the frame is popped from the call stack, and the program resumes execution from the return address stored in the popped frame.

The call stack in Salesforce can be particularly important in debugging, as it can help developers identify where an error occurred and how the program reached that point.


Hands-On Code

				
					public class MyClass {
    static String name = 'Apple Company';
    static {
        System.debug('Static block executed '+name);
        // Country - State
        // Country - Currency
        // Interface - Object
    }
    {
       // Instance Block 
       System.debug('Instance Block Executed');
    }
    public MyClass(){
        System.debug('Constructor Executed');
    }
    // 10
    // 12
    // 167
}
public class CallStackDemo {
    public void methodA(){
        System.debug('Inside Method A');
        System.debug('Inside Method A1');
        System.debug('Inside Method A1');
        methodB();
        System.debug('A');
    }
    public void methodB(){
        System.debug('Inside Method B');
        String methodCOutput = methodC(); // Method Call
        System.debug('B n '+ methodCOutput);
    }
    public String methodC(){
        methodD(); // Method
        System.debug('C');
        return 'Hello I'm from method C';
    }
    public void methodD(){
        methodE();
        System.debug('D');
    }
    public void methodE(){
        System.debug('E');
    }
}
CallStackDemo callStack = new CallStackDemo();
callStack.methodA();
/*
public class Account{
    public String Name;
    public String Phone;
    public String Fax;
    public Account(String Name, String Phone, String Fax){
        this.Name = Name;
        this.Phone = Phone;
        this.Fax = Fax;
    }
}*/
Account accountRecord = new Account();
accountRecord.Name  = 'TEST ACCOUNT';
accountRecord.Phone = '8956421457';
accountRecord.Fax   = '8956421457';
insert accountRecord;
Account accountRecord1 = new Account(
    Name  = 'TEST ACCOUNT WITH CON',
    Phone = '8956421457',
    Fax   = '8956421457'
);
insert accountRecord1;
Laptops apple = new Laptops(null);
				
			

Complete Video

Resources

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading