Apex Classes, Methods, and Sub-Classes: What’s the Difference?

Photo by Ан Нет on Unsplash

One of the questions I get from admins starting to write Apex is: What’s the difference between a class, a method, and a sub-class? It’s an important distinction that’s easy to miss when you're focused on just getting the code to work.

Here’s the breakdown:

  • A Class is the container. Think of it like a toolbox. It can hold properties (data) and methods (actions).

  • A Method is an action the class can perform. It’s like a tool in the box—each method does something specific.

  • A Sub-Class is a class defined inside another class. It’s used for organizing related logic or creating structured data objects within a class.

For example:

public class ReservationUtils {

    public class ReservationData {

        public String name;

        public Date reservationDate;

    }

    public static String formatReservation(ReservationData res) {

        return res.name + ' - ' + res.reservationDate.format();

    }

}

In this example:

  • ReservationUtils is the class

  • ReservationData is a sub-class used for structure

  • formatReservation() is a method that performs an action

Keeping these roles clear makes your Apex code easier to read, maintain, and test. It also sets you up for more modular, reusable development.