Table of Contents
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:
- It has the same name as the class name.
- It doesn’t return value.
- 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
- We don’t have to invoke the constructor explicitly.
- We can call one constructor from another constructor in Apex. This is known as Constructor Chaining.
- Constructors in Apex can be overloaded.
- 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
- 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.
- The constructor doesn’t have any return type, whereas the method has a return type.
- The constructor’s name has to be the same as that of the class name, but the method name can be anything
- The constructor gets invoked when an object is created using a new keyword, whereas the method is invoked through method calls.
- Constructor is used for initializing an object, whereas method includes Apex code to get executed.
- The system implicitly invokes the constructor, whereas the developer invokes the method.
0 Comments