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.
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
1. How does every Apex statement end?
2. Is Apex case-sensitive for identifiers?
3. What convention is typically used for Apex class names?
4. What symbol starts a single-line comment in Apex?
5. What must be declared before using a variable in Apex?
Was this page helpful?
You May Also Like
What Is Apex?
An introduction to Apex, Salesforce's proprietary programming language, covering how it runs on the platform, its core characteristics, and when to use it instead of declarative tools.
Apex Data Types
An overview of Apex's primitive types, collection types (List, Set, Map), sObjects and IDs, and the rules governing type conversion and null handling.
Your First Apex Class
A hands-on walkthrough of writing a simple Apex class, covering class structure, methods, access modifiers, static vs instance members, and how to test it.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics