Constructors In Salesforce

Introduction

Apex is a strongly typed OOPS language which is a proprietary language developed by salesforce.com to allow us to write code that executes on the force.com platform. Since it follows the OOPS concept, it has classes (which contain variables and methods) and objects. Constructors and methods are present in classes.

Constructors in Salesforce

When an instance or object of an Apex class is created, the constructor is called implicitly. It’s not mandatory to write a constructor in the Apex class as, by default, the non-parametrized constructor is present. But if the parameterized constructor is created, then the default constructor won’t be created automatically. It is mainly used to instantiate the variables or data members of the class.

So, writing an apex constructor is optional.

It has the properties mentioned below:

  1. It has the same name as the class name.
  2. It doesn’t return value.
  3. It will be invoked at the time of instance creation.

Syntax

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

 

Example

1. non-parametrized constructor

public class Student {
String studentName;
Integer studentId;
public Student(){
studentName = 'Rashi Sharma';
studentId = 19923;
}
public void print(){
System.debug('Student Name: ' + studentName);
System.debug('Student Id: ' + studentId);
}
}

 

OPEN ANONYMOUS WINDOW

//creation of an object
Student s = new Student();
s.print();

 

2. Parameterized Constructor

public class Student {
String studentName;
Integer studentId;
public Student(String studentName, Integer studentId){
this.studentName = studentName;
this.studentId = studentId;
}
public void print(){
System.debug('Student Name: ' + studentName);
System.debug('Student Id: ' + studentId);
}

OPEN ANONYMOUS WINDOW

//creation of an object
Student s = new Student('Manvi Shah', 12757);
s.print();

 

Benefits of Constructors in Salesforce

  1. We don’t have to invoke the constructor explicitly.
  2. We can call one constructor from another constructor in Apex. This is known as Constructor Chaining.
  3. Constructors in Apex can be overloaded.
  4. We don’t have to create a non-parametrized constructor explicitly, as it comes by default when we create an instance of the class.

Difference between a Constructor and a Method in Salesforce

  1. Constructor is a special method that is called automatically whenever an instance of a class is created, whereas we need to call the normal method explicitly.
  2. The constructor doesn’t have any return type, whereas the method has a return type.
  3. The constructor’s name has to be the same as that of the class name, but the method name can be anything
  4. The constructor gets invoked when an object is created using a new keyword, whereas the method is invoked through method calls.
  5. Constructor is used for initializing an object, whereas method includes Apex code to get executed.
  6. The system implicitly invokes the constructor, whereas the developer invokes the method.

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.