SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex Constructors Simplified

Objects are Constructed. How are they constructed? They are using constructors.

We can not create a new object without invoking the constructors. Constructors are invoked using the new keyword.

They are used to initialize the member variables of the class. Constructors look similar to a method but they can not ever have a return type!

They are many types of constructors.

Let’s discuss the Default and Parameterized Constructors as they are used extensively in Apex.

Default Constructors

Default Constructors have the same name as a class along with the same access modifier as the class.

Default Constructors has no arguments.

For Example :

public with sharing class Player {
    public String name;
    public String role;
    
    // Default Constructor
    public Player() {

    }
}

Declaring Default Constructors is optional. Complier will add it for you don’t add explicitly.

public with sharing class Player {
    public String name;
    public String role;
}

In the above class, we haven’t declared the default constructor explicitly and the compiler will add it for us.

Let’s create objects using default constructors.

public with sharing class CricketTeam {
    
    public void playerList() {
        Player p1 = new Player();
        p1.name = 'MS Dhoni';
        p1.role = 'Wicket Keeper Batsman';

        Player p2 = new Player();
        p1.name = 'Virat Kohli';
        p1.role = 'Batsman';
        
    }
}

In the above class, we are creating objects using the new keyword. Once the object is created its member variables are initialized.

Parameterized Constructors

They are similar to the default constructor with a key difference that Parameterized Constructors accept the arguments or parameters.

For Example :

public with sharing class Player {
    public String name;
    public String role;
    
    // Parameterized Constructor
    public Player(String name, String role) {
        this.name = name;
        this.role = role;
    }
}

In the above code block, we have declared the parameterized constructors with two parameters that is String name and String role. In the parameterized constructors definition we have used this keyword.

this keyword is used to refer to the currently executing object.

You can also relate it to that when we declare this.name it is referring to class level member variable name instead of parameter name which was passed in the constructor.

Let’s create objects using parameterized constructors :

public with sharing class CricketTeam {
    
    public void playerList() {
        Player p1 = new Player('MS Dhoni', 'Wicket Keeper Batsman');
        Player p2 = new Player('Virat Kohli', 'Batsman');
        
    }
}

In the above code block, we have created the objects using parameterized constructors.

Please note that when we declare parameterized constructors default constructors are not added automatically by the compiler. We have to declare it explicitly.

Thus, if your class only has parameterized constructors and you are trying to create objects using default constructors then the compiler won’t agree with it and will throw an error.

For Example :

public with sharing class CricketTeam {
    
    public void playerList() {
        Player p1 = new Player(); // Compliation Error
    }
}

As in the Player class above we have only declared the parameterized constructor so an error will be thrown in the CricketTeam class that – “Constructor not defined: [Player].<Constructor>()” that is no default constructor found.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *