Understanding Constructors in Apex: A Beginner’s Guide

Understanding Constructors in Apex: A Beginner’s Guide 

In the world of Apex programming, constructors play a crucial role in object-oriented design. If you’re just getting started with Salesforce development or brushing up on your Apex skills, understanding constructors is essential for creating and initializing objects effectively. 

🔹 What is a Constructor? 

A constructor is a special type of method in a class that gets executed automatically when an object of that class is created. It’s typically used to initialize variables or perform setup operations needed before the object is used. 

Key Characteristics: 

  • The constructor name must match the class name. 
  • Constructors are always defined with the public access modifier. 
  • Constructors do not return any value, not even void. 
  • Apex allows constructor overloading—you can define multiple constructors in one class. 
  • You can perform constructor chaining, where one constructor calls another within the same class. 

Basic Syntax: 

public ClassName() { 

    // Your initialization logic 

} 

🔹 Types of Constructors in Apex 

Apex supports two main types of constructors: 

  1. Non-Parameterized Constructor (Default Constructor)

This type of constructor doesn’t accept any arguments and typically sets default values for class variables. 

Example: 

public class CustomerDetailsHelper { 

    private Integer customerCode; 

    private String customerName, address, phoneNumber; 

    private Decimal balanceAmount; 

    public CustomerDetailsHelper() { 

        customerCode = 100001; 

        customerName = ‘Sampath Kumar’; 

        address = ‘JP Nagar, Bangalore’; 

        phoneNumber = ‘9988776655’; 

        balanceAmount = 12500; 

    } 

    public void showCustomerDetails() { 

        System.debug(‘Customer Code: ‘ + customerCode); 

        System.debug(‘Customer Name: ‘ + customerName); 

        System.debug(‘Balance Amount: ‘ + balanceAmount); 

        System.debug(‘Customer Address: ‘ + address); 

    } 

} 

Usage: 

CustomerDetailsHelper custHelper = new CustomerDetailsHelper(); 

custHelper.showCustomerDetails(); 

  1. Parameterized Constructor

This constructor accepts input values at the time of object creation, which can then be used to initialize the object’s state. 

Example: 

public class ProductDetailsHelper { 

    private Integer productCode; 

    private String productName, manufacturer; 

    private Decimal unitPrice; 

    private Date manufacturedDate, expiryDate; 

    public ProductDetailsHelper(Integer pCode, String pName, String pManufacturer, Decimal pPrice, Date pmDate, Date peDate) { 

        productCode = pCode; 

        productName = pName; 

        manufacturer = pManufacturer; 

        unitPrice = pPrice; 

        manufacturedDate = pmDate; 

        expiryDate = peDate; 

    } 

    public void showProductInfo() { 

        System.debug(‘Product Code: ‘ + productCode); 

        System.debug(‘Product Name: ‘ + productName); 

        System.debug(‘Unit Price: ‘ + unitPrice); 

        System.debug(‘Expiry Date: ‘ + expiryDate.format()); 

    } 

} 

Usage: 

ProductDetailsHelper prodHelper = new ProductDetailsHelper( 

    2001, ‘Desktop’, ‘DELL Inc.’, 24000, 

    Date.newInstance(2019, 04, 23), 

    Date.newInstance(2034, 04, 22) 

); 

prodHelper.showProductInfo(); 

🔹 Bonus: Constructor Chaining 

Constructor chaining allows one constructor to call another within the same class, providing a clean and modular way to handle different object initialization scenarios. 

Syntax Tip:
Use this() to call another constructor within the same class. 

💡 Final Thoughts 

Constructors make your Apex classes cleaner, more modular, and easier to maintain. Whether you’re setting default values or initializing with user input, constructors are your go-to tool for building reliable, structured Apex code. 

 

Leave a Comment

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