The Apex Master Class

What Is a Method in Apex? (Master Class Ep. 12)

4 min read

Subscribe on YouTube

What Is A Method?

I’ve been saying “method” for about four episodes now while cheerfully never defining it. Let’s fix that.

Back in episode 7 we said a class is a blueprint for a thing or a service. Well:

Methods are the behaviours of that thing. They’re what it can actually do.

Say you’re modelling a computer. What does a computer do? It starts up. It powers down. So your Computer class gets a startUp() method and a powerDown() method, and each of those holds the code that makes it happen.

Apex
public class Computer
{
    private void startUp()
    {
        //all the code that starts up a computer goes in here
    }

    private void powerDown()
    {
        //all the code that shuts it down goes in here
    }
}

That’s genuinely the whole concept. A class is the noun, its methods are the verbs.


Taking A Method Apart

Every method has the same four bits. Here’s one with all of them doing something interesting:

Apex
public Integer addValues(Integer valueOne, Integer valueTwo)
{
    return valueOne + valueTwo;
}

1. The access modifierpublic
This decides who’s allowed to call it. private means only code inside this same class can. public means anything in your org can. There’s a whole run of episodes coming on these (public, private, protected, global), so for now: default to private unless something outside genuinely needs to call it.

2. The return typeInteger
Remember Apex is strongly typed, so you have to promise up front what you’re handing back. This one promises an Integer.

3. The nameaddValues
Name it after what it does. Verbs are good here. addValues, calculateDistance, sendEmail. If you can’t name a method easily, that’s usually your code telling you it’s doing too many things at once. Listen to it.

4. The parameters(Integer valueOne, Integer valueTwo)
Values you hand in for the method to work with. Each one needs a type and a name, same as any variable. You can have as many as you want, or none at all.


The Void Keyword

Sometimes a method does something without handing anything back. Powering down a computer doesn’t produce a value, it just… powers down the computer.

For those, the return type is void:

Apex
public void powerDown()
{
    System.debug('Powering down!');
    //no return statement needed
}

void literally means “this gives you nothing back.” It’s not a type, it’s the absence of one. And if you try to return a value from a void method, Apex will stop you.


Calling A Method

Writing a method does nothing on its own. It just sits there being a method. You have to call it:

Apex
public class Calculator
{
    public void runIt()
    {
        //call addValues, catch what it returns
        Integer total = addValues(5, 10);

        System.debug('The total is ::: ' + total);   //prints 15
    }

    private Integer addValues(Integer valueOne, Integer valueTwo)
    {
        return valueOne + valueTwo;
    }
}

Notice we caught the return value in a variable. If you call a method that returns something and just ignore it, Apex won’t complain, but you’ve also achieved nothing, so.

Quick reminder from episode 9: those Integer parameters are passed by value, so changing them inside the method won’t touch the originals. Pass in a Contact or a List and it’s a different story entirely.


One Method, One Job

Small thing to plant now, because it’ll save you real pain later: a method should do one thing.

It is very tempting, especially when you’re moving fast, to write one glorious 400 line method that queries records, transforms them, validates them, sends an email and updates the database. It works! It’s also completely untestable, impossible to reuse, and a nightmare for whoever inherits it.

Small methods with clear names are easier to test, easier to reuse, and easier to read at 2am when production is on fire. We go much deeper on this in the separation of concerns series, but start building the habit now.


What’s Next

So: methods are what your class can do. Access modifier, return type, name, parameters. Use void when there’s nothing to give back. Keep them small.

The Apex Developer Guide covers methods in detail here if you want more.

In the next episode we look at a very special method called the constructor, which runs the moment your class is created.

See you next time!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture