What Is A Variable?
Welcome back! Last time we made an empty Apex class. Today we start putting things in it, beginning with variables.
Simply put, a variable is a way to store a value so you can use it later on in your code. That’s it. That’s the whole concept.
As for why you’d want that: any time you’re building something real, you need somewhere to hold bits of data while your code runs. The distance between two points. The amount on an Opportunity. A Contact’s name. A running total. You need somewhere to put those things, and that somewhere is a variable.
Making One
Here’s a variable:
Integer distance = 1;Three parts, let’s take them one at a time.Integer is the type. Remember from episode 3 that Apex is strongly typed, so you have to tell it what kind of value this variable will hold. An Integer is a whole number with no decimal point.distance is the name. This can be almost anything. I could have called it taco. I could have called it batman. Apex genuinely does not care.
But you should care, and so should whoever reads this code in eight months (which is very often future you, who will remember nothing). Name your variables after what they actually hold. distance tells the next person something. batman does not.= 1 assigns the value. Anything that fits the constraints of an Integer works here.
You Don’t Have To Assign It Right Away
Sometimes you want the variable to exist now but you won’t know its value until later. That’s fine:
//Declared, but no value assigned yet
Integer distance;
//Explicitly set to nothing
Integer otherDistance = null;Both are legal. null is Apex’s way of saying “nothing.” It’s worth getting comfortable with that word now, because null is responsible for a truly heroic percentage of the errors you’ll hit in your career. When you eventually meet the dreaded System.NullPointerException, this is what it’s complaining about.
Using It
Once a variable exists, you reference it by name. Here it is inside a class:
public class MappingService
{
public void calculateDistance()
{
Integer distance = 1;
System.debug('This is the distance ::: ' + distance);
}
}System.debug() prints a value to the debug log, and it’s going to be your closest companion for the rest of this series. Anywhere you write distance, Apex swaps in whatever that variable currently holds.
To actually run this, open the anonymous Apex window in your IDE and execute:
new MappingService().calculateDistance();Don’t panic about that line. We haven’t covered methods or instantiating classes yet, and both get their own episodes shortly. For now just run it and look at the debug log, where you’ll see:
This is the distance ::: 1Variables Vary
The clue is in the name. You can change a variable’s value whenever you like:
Integer distance = 1;
distance = 2; //now it's 2
distance = distance + 2; //now it's 4That third line confuses people the first time, because it doesn’t look like maths. Read it right to left: take the current value of distance, add 2, then store the result back into distance. The right side runs first, then the assignment happens.
You can also compare variables to each other:
Integer distanceOne = 1;
Integer distanceTwo = 2;
Boolean areTheyEqual = (distanceOne == distanceTwo); //falseNote the double equals. One equals sign assigns a value, two equals signs compare values. Mixing those up is a rite of passage. We’ll cover it properly in the episode on operators.
What’s Next
So a variable is a named store for a value, you declare it with a type, and you can read and change it anywhere it’s in scope.
There’s quite a bit more to say about variables, which is why they get several episodes. Coming up: what primitive and non-primitive types are, whether they get passed by value or by reference, and what variable scope means. If you want to read ahead, the Apex Developer Guide covers variables here.
In the next episode we’ll go through the primitive data types and why they’re called “primitive” in the first place.
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