100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Apex Syntax and Variables

Covers the foundational rules of Apex syntax, how to declare and scope variables, naming conventions, and how comments structure readable code.

FoundationsBeginner7 min readJul 10, 2026
Analogies

Basic Apex Syntax

Apex syntax closely mirrors Java: every statement ends with a semicolon, and code blocks are delimited by curly braces. The language is case-insensitive for identifiers and keywords, though the strong convention is to use camelCase for variables and methods and PascalCase for class names, purely for readability, not because the compiler requires it.

🏏

Cricket analogy: Like scoring conventions being universally understood across countries - a 'wide' means the same thing in Mumbai or Melbourne, just as a semicolon means 'end of statement' in every Apex file, everywhere.

Declaring and Using Variables

A variable is declared with its data type first, followed by a name and an optional initial value, for example Integer count = 0; or String name = 'Trailhead';. Because Apex is strongly typed, the variable must be declared before it's used, and its type never changes after that - trying to assign a value of a different type later results in a compile error.

🏏

Cricket analogy: Like reserving a specific batting slot, say No. 3, for a particular type of player before the match starts, rather than deciding who bats where on the fly.

Naming Conventions and Case Sensitivity

Apex identifiers are case-insensitive, so myVar and MYVAR refer to the exact same variable to the compiler, but strong convention still favors camelCase for variables and methods and PascalCase for class names, purely to keep code readable and consistent across a team.

🏏

Cricket analogy: Like how 'MS Dhoni' and 'ms dhoni' would refer to the same player in a scorecard database regardless of capitalization, even though official scorecards still follow one consistent style.

Comments and Code Structure

Apex supports single-line comments starting with // and multi-line comments wrapped in /* */; both are ignored entirely by the compiler and exist purely to help human readers understand intent. Code itself is organized into classes and methods, with curly braces defining the scope of each block, and consistent indentation - while not syntactically required - makes that structure much easier to follow.

🏏

Cricket analogy: Like a commentator's aside explaining strategy during a lull in play - it doesn't affect the game itself, just as a comment doesn't affect code execution but aids understanding.

apex
public class OrderCalculator {
    // Applies a discount to a total (single-line comment)
    public static Decimal applyDiscount(Decimal total, Decimal discountPercent) {
        /* Multi-line comment:
           discountPercent is expressed as a whole number, e.g. 10 for 10% */
        Decimal discountAmount = total * (discountPercent / 100);
        Decimal finalTotal = total - discountAmount;
        return finalTotal;
    }
}

Apex reserves certain words, such as class, trigger, list, null, and this, that cannot be used as identifiers. The compiler throws a syntax error if you try to name a variable after a reserved keyword.

Because Apex is case-insensitive, declaring two variables that differ only in case, such as myTotal and MyTotal, in the same scope causes a compile-time 'variable already defined' error - never rely on case alone to distinguish identifiers.

  • Every Apex statement must end with a semicolon; code blocks are delimited by curly braces.
  • Apex is case-insensitive, but camelCase (variables/methods) and PascalCase (classes) conventions remain essential for readability.
  • Variables must be declared with an explicit data type before they can be used.
  • Variable scope is limited to the block in which it is declared.
  • Single-line comments use //, multi-line comments use /* */.
  • Indentation isn't syntactically required but greatly improves code readability.
  • Reserved keywords and invalid identifier characters, like starting with a digit, cause compile errors.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ApexSalesforceStudyNotes#ApexSyntaxAndVariables#Apex#Syntax#Variables#Declaring#StudyNotes#SkillVeris#ExamPrep