What is Constructor in Apex Programming? | Salesforce Apex Guide

In Apex programming, a Constructor is a unique code used to set up a newly created object. It is called right after the memory for the object is set aside. It can be used to set the default or required values for an object when it is first created. The coder doesn't have to write a constructor for the class, but they can if they want to. A default one is used if your Apex class doesn't have a constructor. And it doesn't take any arguments. The compiler sets the member variables to their default values if a class does not have a user-defined constructor. If a class doesn't have a user-defined constructor, a default, no-argument, public constructor is used in this particular case. It is called constructor overloading" when a class has more than one constructor, each of which takes different arguments.

For Example : 

public class TestObject
 {
   \\The no argument constructor

    public TestObject()
     {
       \\code
      }
  }

Rules to Create a Constructor

  • It should have the name of the class 
  • It should be used to instantiate the data members present in the class. 
  • It should return a value
  • Use of public access specifier 
  • The method can only be invoked at the time of object creation

Syntax of Apex Constructor

A constructor's syntax is similar to that of a method, but it is not the same as a method in terms of definition because it doesn't have an explicit return type and the object that is created from it doesn't get to use it.   

public class TestClass{
public TestClass(){ // non-parameterized constructor
//code
}
}
//object creation
TestClass tc = new TestClass(); 

dont miss out iconDon't forget to check out: How to Expose Apex Class as a SOAP Service in Postman? | Salesforce Developer Guide

Types of Constructors in Apex Programming

1. Default Constructor

In the example given above, the apex class has no constructor. So, when we create an object, like a class, the Apex compiler makes a default constructor. 

For Example:

Public class Example
 {
  
  }
 Example e = new Example();

2. Non-parameterized Constructor

Non-parameterized constructor is a special kind of constructor that doesn't take any parameters. The parameters mentioned before are basically the values passing inside a constructor. 

For Example:

public class Example
{
  Integer number;
   String Name;

   public Example() //Non-parametarized Constructor
   {
    number=10;
     name=Salesforcekid;
   }

}

3. Parameterized Constructor

Now, let's make a second class that uses our constructor and takes a string as an argument. It is a builder with parameters. That means that we'll ask the user for information and then match it up with our variable.

To Create a Constructor Following Points Should Be Taken Care Of:

If the method name is different from the class name, it will cause an error when you try to save.

Access specifier must be public; if it is private, the method can't be seen. 

A Constructor is called when a new object is made.

There must not be a specified return type.

For Example:

public class Example
 {
   Integer number;
   String Name;
public Example(Integer x, String myname) //Parametarized Constructor
 {
   number = x;
   Name = myname;
 }

Benefits of Apex Constructors in Salesforce

  • There is no requirement for us to invoke the constructor directly.
  • In Apex, we are able to invoke one constructor from another constructor. This technique is referred to as "Constructor Chaining." 
  • Overloading a constructor's default behaviour is possible in Apex.
  • When we make an instance of the class, the non-parametrized constructor is created for us automatically, and we do not need to do any additional steps to make it. 

dont miss out iconCheck out another amazing blog by Emizen here: Top 20 Factors To Consider When Choosing CRM For Small Business | Salesforce

Constructor in Apex Programming with Example

In Apex programming, we must first write an Apex class before we can write an Apex Constructor. To write an Apex class, you need to sign in to Salesforce, go to the developer console, and click on the file button. 

Select the new Apex class and name the class.

Apex Class

public class Employee {
    string Employeename;
    Integer EmployeeNo;
    Public Employee() {
        EmployeeName = 'Prasant kumar';
        EmployeeNo = 1015;
    }
    public void Show() {
        System.debug('Emplyeename is' +EmployeeName);
        System.debug('EmplyeeNo is' +EmployeeNo);
    }
}

Now open the anonymous block.

How Can Emizentech Help You? 

The use of Salesforce in modern enterprises has been revolutionary. Salesforce platforms revolutionize organizations by uniting their staff, suppliers, and consumers under one roof. We are the premier Salesforce development firm functioning in the global market. Our services span the full spectrum of the Salesforce platform, from Salesforce1 mobile apps and API creation to bespoke Salesforce implementations and connections with other platforms. Our Salesforce IoT developers assist businesses in establishing long-lasting bonds with their clientele by utilizing several different technological modalities and the top Salesforce standards in the industry.  

Responses

Popular Salesforce Blogs