Apex Constructors

Apex Constructors

A constructor in Apex is a special method that runs automatically when an object of a class is created. It’s used to initialize the values of class variables when the object is made. 

A constructor: 

• Has the same name as the class.
• Does not return any value, not even void.
• Can be default (no parameters) or parameterized (with input values).

1. Default Constructor

A default constructor doesn’t take any parameters. 

Example:

public class Student {
public String name;
public Integer age;

// Default constructor
public Student() {
name = ‘Not Set’;
age = 0;
}

public void showDetails() {
System.debug(‘Name: ‘ + name);
System.debug(‘Age: ‘ + age);
}
}

Student s1 = new Student();
s1.showDetails();

Code Explanation:

The Student() constructor runs when new Student() is called.
It sets default values for name and age.
s1.showDetails() prints those default values.

Output:

Name: Not Set
Age: 0

2. Parameterized Constructor

This constructor takes values and sets them while creating the object. 

Example:

public class Book {
public String title;
public String author;

// Parameterized constructor
public Book(String t, String a) {
title = t;
author = a;
}

public void showInfo() {
System.debug(‘Title: ‘ + title);
System.debug(‘Author: ‘ + author);
}
}

Book b1 = new Book(‘Atomic Habits’, ‘James Clear’);
b1.showInfo();

Code Explanation:

Book(String t, String a) is a constructor with parameters.
new Book(‘Atomic Habits’, ‘James Clear’) sets the title and author.
b1.showInfo() prints the values.

Output:

Title: Atomic Habits
Author: James Clear

3. Multiple Constructors (Overloading)

Apex supports constructor overloading, meaning you can define multiple constructors in one class with different parameters. 

Example:

public class Employee {
public String name;
public Integer salary;

// Default constructor
public Employee() {
name = ‘Unknown’;
salary = 0;
}

// Parameterized constructor
public Employee(String n, Integer s) {
name = n;
salary = s;
}

public void printInfo() {
System.debug(‘Name: ‘ + name);
System.debug(‘Salary: ‘ + salary);
}
}

Employee e1 = new Employee();
e1.printInfo();

Employee e2 = new Employee(‘Alice’, 50000);
e2.printInfo();

Code Explanation:

Two constructors: one with no arguments, one with two arguments.
Depending on how the object is created, the correct constructor is called.

Output:

Name: Unknown
Salary: 0
Name: Faiz
Salary: 50000

Constructors Save Time

Constructors help us quickly set up an object with default or given values at the moment it’s created — instead of setting each value manually one by one. 

Real-life Analogy 

Imagine you’re ordering a custom pizza online. 

Without a constructor: you call and say, “Add cheese. Now add olives. Now add mushrooms…”
With a constructor: you choose “Cheese + Olive + Mushroom” from a preset combo with one click.

Constructors = Instant Setup 

Without constructors = Manual Setup Step-by-Step  

Tasks for Practice

Task 1: Create a class Car with properties: brand and model. Use a constructor to set the values and print them using a method.

Task 2: Create a class Person with a default constructor that sets name to "No Name" and age to 0. Print the data.

Task 3: Create a class Movie with multiple constructors. One should set only the title, and the other should set both title and rating. Print the values.

Task 4: Create a class Rectangle with length and width. Use a constructor to set the values and create a method area() to return area.

Task 5: Create a class called Car with two properties:

• String brand
• Integer modelYear Create a default constructor that assigns default values, and print them by creating an object.

Task 6: Create a class called Employee with:

String name
Decimal salary Write a parameterized constructor to initialize these fields. Then, create an object by passing values and print the data.

Task 7: Create a class Rectangle with properties length and width.

Write two constructors:
   One that takes no parameters and assigns default values
   One that takes two parameters to assign custom values
        Create both types of objects and print the area.

Task 8: Create a class Person with name and age.

Inside the constructor, call a method displayInfo() that prints the name and age. 
Then, create an object to trigger the output.