Salesforce Menu


Constructor in Apex is a special type of method that is used to initialize the object.
Apex constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.


1. Each and every time an object is created using the new() keyword, at least one constructor of the class is get called.

2. It’s called a default constructor in the class if there is no constructor supplied by yourself, it is the responsibility of Apex compiler is to supply a default constructor into your class by default.

Rules for creating Apex constructor:

There are basically two rules defined for the constructor.

  • Constructor name always must be same as its class name
  • Constructor must have no explicit return type

There are two types of constructors:

  • Default constructor (no-arg constructor)
  • Parameterized constructor


Apex Default Constructor

A constructor that has no parameter is known as default constructor.

Syntax of default constructor.

public class ApexConstructor{
 public ApexConstructor() {
  System.debug(‘This is Apex Default Constructor’);


Apex Parameterized Constructor

A constructor which is having number of arguments, known as Parameterized constructor.

A constructor which is having number of arguments, known as Parameterized constructor.
public class ApexConstructor{
 public ApexConstructor(Integer no){
  System.debug(‘This is Apex parameterized Constructor ‘ +no);


Constructor Overloading in Apex

In Apex, a constructor is just like a method but without return type. It can also be overloaded like Apex methods.

Constructor overloading in Apex is a technique of having more than one constructor with same name with different parameter lists. They are basically arranged in a way that each constructor performs a different task as per need. They are differentiated by the compiler by the number of parameters/arguments in the list and their types.

Subscribe Now