How Call Stack works in Salesforce in Hindi

on

|

views

and

comments

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

				
					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 Singhhttps://www.pantherschools.com/
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
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential