Beginner 15 min read

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;
  1. Integer age = 30; — declares a new variable named age of type Integer, and assigns it the value 30 in 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
  1. Integer age; — declares age as an Integer, with no value yet — Apex gives it null until something is assigned.
  2. age = 30; — assigns 30 to the already-declared age. No type is repeated here; Apex already knows age's type from line 1.
  3. age = 31; — reassigns age to a new value, replacing the 30 it 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 age partway 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
  1. Integer age = 30; — declares age and assigns it 30 in one step.
  2. System.debug(age); — logs the current value of age — 30 — to the debug log.
  3. (blank line, separating the first pair of statements from the second)
  4. age = 31; — reassigns age to 31. No Integer keyword needed again — Apex already knows age's type.
  5. System.debug(age); — logs age again; this time it prints 31, 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;

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

What Is a Variable? Quiz

1. What does it mean that Apex is "strongly-typed"?

2. A variable can be declared without immediately assigning it a value.

Log in to submit the quiz and save your score.

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.