Sub topic 1
Declaring an Apex class
A class is the basic unit of code organization in Apex. It bundles data (fields) and behavior (methods) under one name. This chapter covers the parts of a class declaration and how to create and use an object from it.
Prerequisites
This chapter assumes you already know:
- Basic Apex syntax: variables, data types, and control flow.
- How to use collections such as
ListandMap. - That Apex runs on Salesforce inside transactions.
- Basic object-oriented ideas, such as what an object and a method are.
The shape of a declaration
A class declaration has an access modifier, the class keyword, a name, and a body in braces. The body holds fields, constructors, and methods.
public class OrderSummary {
private String customerName;
private Decimal total;
public OrderSummary(String customerName, Decimal total) {
this.customerName = customerName;
this.total = total;
}
public String describe() {
return customerName + ' owes ' + total;
}
}
// Usage
OrderSummary summary = new OrderSummary('Ana', 120.5);
System.debug(summary.describe()); // Ana owes 120.5The class has two fields, one constructor, and one method. The new keyword creates an object (an instance) and runs the constructor. Each object keeps its own copy of the fields.
| Part | Example | Purpose |
|---|---|---|
| Access modifier | `public` | Controls which code can see the class |
| Name | `OrderSummary` | Identifies the class in your org |
| Field | `private Decimal total;` | Stores state |
| Constructor | `public OrderSummary(...)` | Sets up a new object |
| Method | `public String describe()` | Defines behavior |
Rules for top-level classes
- A top-level class must be declared
publicorglobal. Aprivatetop-level class is allowed only for test classes marked@isTest. Access modifiers get a fuller treatment in the next chapter. - Class names are case-insensitive and must be unique in your org (or namespace).
OrderSummaryandordersummarywould collide. - Each top-level class lives in its own file. In a Salesforce DX project,
OrderSummaryis stored asOrderSummary.clswith a companionOrderSummary.cls-meta.xmlfile that records the API version and status. - By convention, class names use UpperCamelCase, and field and method names use lowerCamelCase.
Members default to private
If you leave the modifier off a field or method, it is private to the class. That is a good default. Expose only what other code needs, and keep the rest inside.
public class Temperature {
Decimal celsius; // no modifier: private
public Temperature(Decimal celsius) {
this.celsius = celsius;
}
public Decimal toFahrenheit() {
return celsius * 9 / 5 + 32;
}
}Code outside the class can call toFahrenheit() but cannot read celsius directly.
The implicit default constructor
If you declare no constructor, Apex supplies a public one with no parameters. As soon as you write any constructor, that implicit one disappears. This causes a common error: you add a constructor with parameters, and existing code that calls new OrderSummary() stops compiling. Add an explicit no-argument constructor if you still need that form.
Common mistakes
- Declaring a top-level class without
publicorglobal. - Using a name that differs from the file name, or one that is already taken.
- Adding a parameterized constructor and forgetting that the implicit default constructor is gone.
- Making every field
publicwhen only a few need to be visible.
Exercise
You have a class Invoice with only the constructor public Invoice(Decimal amount). A teammate writes Invoice i = new Invoice(); and gets a compile error. Why, and how do you fix it?
Show answer
Once a class declares any constructor, Apex no longer provides the implicit no-argument constructor. Fix it by adding an explicit public Invoice() { } (or by changing the call to pass an amount).