What Is a Variable?
By the end of this lesson, you'll be able to:
- Explain what a variable is using a real-world analogy
- Declare a variable in Apex with a type and a name
- Assign and update a variable's value
- Explain why Apex requires a variable's type to be declared up front
Prerequisites: Module 2: "Understanding the Salesforce Platform"
A labelled box
Picture a labelled storage box. The label tells you what's allowed inside — a box labelled "Invoices" shouldn't have a stapler in it. A variable works the same way: it has a name (the label) and a type (what's allowed inside), and it holds one value at a time.
Integer age = 30;
Integer age = 30;— declares a new variable namedageof typeInteger, and assigns it the value30in the same statement. The type comes first, then the name, then=, then the value, ending in a semicolon.
Declaring vs. assigning
Declaring a variable creates the box and labels it. Assigning puts something inside. You can do both at once (as above), or separately:
Integer age; // declare — an empty, labelled box
age = 30; // assign — put a value in it
age = 31; // reassign — swap out what's inside
Integer age;— declaresageas anInteger, with no value yet — Apex gives itnulluntil something is assigned.age = 30;— assigns30to the already-declaredage. No type is repeated here; Apex already knowsage's type from line 1.age = 31;— reassignsageto a new value, replacing the30it held a moment ago.
An Integer box can hold different Integer values over time — but never a String. That restriction is what "strongly-typed" means.
Why Apex insists on this
Some languages let a variable hold anything, of any type, at any time. Apex deliberately doesn't — the type is fixed the moment you declare it. This might feel like extra ceremony coming from nothing, but it catches real mistakes early: if you accidentally try to put text into a box labelled "numbers only," Apex refuses to even compile your code, rather than letting the mistake slip through and cause a confusing failure later.
Naming rules
A variable name must start with a letter, can contain letters/numbers/underscores, and can't be an Apex reserved word (like class or if). Beyond the compiler's rules, good names are descriptive — age tells you far more than a or x, and this matters enormously once your classes grow past a few lines.
Common mistakes
- Reusing a variable name for something unrelated. Reassigning
agepartway through to mean something else entirely (say, an account ID) makes code far harder to read — pick a new variable instead. - Forgetting the semicolon. Every Apex statement ends in
;— a missing one is one of the most common first-week compiler errors.
Trade-offs and when NOT to use this
There's no "opting out" of typing a variable in Apex — but you do choose how specific to be. Declaring something as the general-purpose Object type instead of a specific type like Integer is technically legal but throws away the compiler's ability to catch mistakes for you. Prefer the most specific type that fits, always.
Declaring, assigning, and reassigning
Integer age = 30;
System.debug(age); // 30
age = 31;
System.debug(age); // 31
Integer age = 30;— declaresageand assigns it30in one step.System.debug(age);— logs the current value ofage—30— to the debug log.- (blank line, separating the first pair of statements from the second)
age = 31;— reassignsageto31. NoIntegerkeyword needed again — Apex already knowsage's type.System.debug(age);— logsageagain; this time it prints31, since line 4 changed it.
Exercise
Declare an Integer variable called yearsOfService, assign it the value 5, then debug it. Then reassign it to 6 and debug it again.
Show hint
Declaration and assignment can happen on the same line: Integer yearsOfService = 5;
Exercise
Coding challenge: declare two Integer variables, add them together into a third variable, and debug the result.
Show hint
The + operator works on Integers just like you'd expect from arithmetic.
What Is a Variable? Quiz
Leaderboard
- 1. Monde Masiza 2/2
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A variable is a labelled, typed box for holding one piece of data while your code runs. Apex is strongly-typed, meaning you must state up front what kind of value a variable will hold — this single fact shapes almost every line of Apex you'll write from here on.