The Apex Master Class

When to Use the Public Keyword in Apex (Ep. 23)

3 min read

Subscribe on YouTube

What Does Public Mean?

Last episode we covered global and I told you to use it as little as possible. Today’s is the one you’ll actually type most days.

public means anything inside your namespace can use it.

For most of us, “namespace” just means “your org.” If you’re not building a managed package, public effectively means “any Apex in this org can call this.”

Apex
public class OpportunityService
{
    public void closeOpportunity(Opportunity opp)
    {
        opp.StageName = 'Closed Won';
    }
}

//Any other class in your org can now do this
new OpportunityService().closeOpportunity(myOpp);

Public vs Global

The distinction only really bites when namespaces are involved.

If you’re working in a normal org with no namespace, public and global look almost identical in practice. Both let anything in your org call the thing.

The moment you’re in a managed package, they diverge completely. Your package gets its own namespace, and public stops dead at that boundary. A subscriber org can’t call your public methods at all, only your global ones.

Which is actually a rather good thing. It means public is your “internal to my package” access level, and you can freely refactor public methods between versions without breaking anybody. Change a global one and you can’t ship the release.


When To Use It

Public is the right choice for anything that’s genuinely part of a class’s public interface – the stuff you actually want other classes calling.

Apex
public class EmailService
{
    //This is the interface. Other code calls this.
    public void sendWelcomeEmail(Contact cont)
    {
        String body = buildEmailBody(cont);
        deliver(cont.Email, body);
    }

    //These are implementation details. Nobody else's business.
    private String buildEmailBody(Contact cont) { ... }
    private void deliver(String toAddress, String body) { ... }
}

One public entry point, two private helpers. Anyone using this class sees exactly one method and doesn’t have to care how the sausage gets made.

That split, a small public surface over private internals, is called encapsulation. It was one of the four pillars of OOP I mentioned way back in episode 3, and this is it in practice. Nothing mystical about it.


Public Classes, Private Members

Worth being clear that the class modifier and the member modifiers are separate decisions.

A public class with private methods is completely normal and very common. It means “other code can use this class, but only through the doors I’ve deliberately left open.”

One thing that trips people up: if you don’t write an access modifier, Apex defaults to private.

Apex
public class MyClass
{
    String someVariable = 'hello';        //this is PRIVATE
    private String otherVariable = 'hi';  //identical, just explicit
}

The default is sensible, but write it out anyway. Explicit beats implicit, and the next person reading it shouldn’t have to remember Apex’s defaults to know what’s going on.


Don’t Just Make Everything Public

I know it’s tempting. Mark everything public, never think about access again, nothing ever fails to compile. Lovely.

Here’s what you’re actually signing up for. Every public method is a method some other class might start calling. Six months later you want to change it, and now you have to find every caller and check you haven’t broken them. Whereas a private method can be renamed, rewritten or deleted whenever you like, because you already know every place it’s used: this class.

The smaller your public surface, the easier your code is to change. That’s the whole argument.

Practical rule: start everything private, and promote to public only when something outside genuinely needs it. Loosening access later takes five seconds. Tightening it means an archaeology expedition.


What’s Next

Public: usable anywhere in your namespace, the default choice for a class’s real interface, and worth being a bit stingy with.

In the next episode we do protected, which is the one most people have never knowingly used.

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