Beginner 20 min read

Constructors

By the end of this lesson, you'll be able to:

  • Explain what a constructor is and when it runs
  • Write a custom constructor that accepts parameters
  • Distinguish the default constructor from a custom one

Prerequisites: "Creating Your First Class"

The default constructor

public class EventTicket {
    public String eventName;
    public Decimal price;
}

EventTicket ticket = new EventTicket(); // uses the invisible default constructor
  1. public class EventTicket { — the blueprint, with no constructor written.
  2. public String eventName; — a field.
  3. public Decimal price; — another field.
  4. } — closes the class.
  5. (blank line)
  6. EventTicket ticket = new EventTicket(); — every class you write gets a free, invisible "default constructor" — a no-parameter new ClassName() that creates an empty object with every field left at its default (usually null). This is what every lesson so far has actually been using.

Writing a custom constructor

public class EventTicket {
    public String eventName;
    public Decimal price;

    public EventTicket(String eventName, Decimal price) {
        this.eventName = eventName;
        this.price = price;
    }
}

EventTicket ticket = new EventTicket('Tech Conference', 1200);
  1. public class EventTicket { — the blueprint.
  2. public String eventName; — a field.
  3. public Decimal price; — another field.
  4. (blank line)
  5. public EventTicket(String eventName, Decimal price) { — a constructor: a method with the same name as the class and no return type.
  6. this.eventName = eventName; — this.eventName refers to the object being built; eventName (without this.) refers to the incoming parameter — this is what tells them apart when the names match.
  7. this.price = price; — same pattern for the price.
  8. } — closes the constructor.
  9. } — closes the class.
  10. (blank line)
  11. EventTicket ticket = new EventTicket('Tech Conference', 1200); — now new takes arguments, which flow straight into the constructor's parameters.

Once you write one constructor, the default disappears

EventTicket broken = new EventTicket(); // compile error!
  1. EventTicket broken = new EventTicket(); — as soon as a class defines its own constructor (as EventTicket now does), Apex stops providing the free no-argument one automatically — this line no longer compiles. If you still want a no-argument option, you have to write it explicitly, alongside your custom one.

Exercise

Add a constructor to the Book class from the previous lesson that accepts title and pageCount as parameters and sets them using this.

Show hint

public Book(String title, Integer pageCount) { this.title = title; this.pageCount = pageCount; }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Constructors Quiz

1. What happens to the default no-argument constructor once you write your own custom constructor?

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 1/1

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A constructor is special code that runs automatically when an object is created with new — it's the natural place to set up an object's initial data.