What Does Static Mean?
I’ll be straight with you up front: static is one of those keywords that seems perfectly obvious until somebody asks you to explain it, at which point you discover you’ve been running on vibes for three years.
So let’s do it properly. Here’s the one sentence version:
Static things belong to the class itself. Non-static things belong to each individual instance.
Remember from episode 14 that every instance you create gets its own private copy of the class variables. Three Computers, three separate sets of values.
Static breaks that. A static variable isn’t copied per instance. There’s exactly one of it, shared by everything, for the whole transaction.
Seeing The Difference
public class Counter
{
public Integer instanceCount = 0; //one per instance
public static Integer staticCount = 0; //ONE. Total. Forever.
public void increment()
{
instanceCount++;
staticCount++;
}
}Now watch what happens with two instances:
Counter counterOne = new Counter();
Counter counterTwo = new Counter();
counterOne.increment();
counterOne.increment();
counterTwo.increment();
System.debug(counterOne.instanceCount); //2
System.debug(counterTwo.instanceCount); //1
System.debug(Counter.staticCount); //3Each instance counted only its own calls. The static variable counted all of them, because there’s only one of it and both instances were poking the same value.
Notice how we read it too: Counter.staticCount, using the class name. Not an instance name. You don’t need an instance to touch a static member, because it doesn’t belong to any instance in the first place.
Static Methods
Same idea. A static method belongs to the class, so you call it without ever building an object:
public class MathUtils
{
public static Integer addValues(Integer one, Integer two)
{
return one + two;
}
}
//No 'new' anywhere. Just call it.
Integer total = MathUtils.addValues(5, 10);You’ve been using static methods since episode 8 without realising it. System.debug() is static. So is String.isBlank(). That’s why you never have to write new System(), which would be a deeply strange thing to type.
One rule that catches people: a static method cannot touch non-static members.
public class Broken
{
public String instanceVariable = 'hello';
public static void doThing()
{
//COMPILE ERROR. Which instance would it even mean?
System.debug(instanceVariable);
}
}And when you think about it, that has to be true. A static method can run with zero instances in existence. Which instance’s variable would it read? There isn’t one. The compiler is being reasonable here, even if it doesn’t feel like it at the time.
It works fine the other way round, though. Non-static methods can happily use static members.
Where Apex Differs From Java And C#
If you’re coming from another language, read this bit twice, because it confused me for an embarrassingly long time.
In Java or C#, a static variable lives as long as the application does. Set it once and it stays set for days.
In Apex, statics only live for the duration of a single transaction. When your transaction ends, every static variable is wiped. The next transaction starts completely fresh.
This is a direct consequence of Salesforce being multi-tenant, which we talked about back in episode 2. Your code doesn’t get a long running process to itself. It gets a slice of shared infrastructure for one transaction and then it’s evicted.
So don’t reach for a static variable expecting a cache that survives between requests. It won’t. That’s what Platform Cache is for, and I’ve got a separate post on that.
What statics are perfect for is sharing state within one transaction, which turns out to be extremely useful. The classic example is stopping a trigger from firing itself in an infinite loop:
public class TriggerControl
{
public static Boolean hasAlreadyRun = false;
}Because it’s static, every bit of code in the transaction sees the same flag. Because it resets between transactions, the next save starts clean. Exactly the behaviour you want, and it works precisely because Apex statics are transaction scoped.
What’s Next
Static belongs to the class, non-static belongs to the instance, static methods can’t see instance members, and in Apex statics die at the end of the transaction.
The official docs on static and instance members are here.
Knowing what static is and knowing when to use it are two different things, so the next episode is entirely about the second one.
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