Video 20 min Free preview

Apex Language Fundamentals

Apex syntax, types, variables, control flow, and how Apex executes on the multitenant platform.

Advertise with us 728 × 90

Apex language fundamentals

Apex is a strongly-typed, object-oriented language that runs on Salesforce servers. Its syntax is deliberately Java-like, so classes, methods, variables, and control flow will feel familiar. What makes it different is the environment: Apex is multitenant, meaning your code shares infrastructure with every other customer, so the platform enforces strict governor limits to keep any one tenant from monopolising resources. Writing good Apex is largely about respecting those limits.

Apex is case-insensitive and statements end with semicolons. Primitive types include Integer, Long, Decimal, Double, Boolean, String, Date, Datetime, and Id. sObjects represent records — Account, Contact, and your custom __c objects are all types you can declare and instantiate. Control flow uses the usual if/else, for, while, and switch on statements.

public class GreetingService {
    // A simple method returning a String
    public static String greet(String name) {
        if (String.isBlank(name)) {
            name = 'there';
        }
        return 'Hello, ' + name + '!';
    }
}

// Invoke it from anonymous Apex or a test
String message = GreetingService.greet('Ada');
System.debug(message); // Hello, Ada!

Notice a few conventions. Methods and classes declare an access modifier (public, private, global). static methods belong to the class rather than an instance. System.debug() writes to the debug log, your primary tool for inspecting execution. Apex runs in a specific context — a trigger, a controller, a batch job, an anonymous script — and that context determines which limits apply.

The single most important habit to build now is thinking in bulk. Apex frequently runs against many records at once, so you'll write code that operates on collections rather than one record at a time. Keep that in mind as we cover collections next; almost every governor limit problem traces back to doing per-record work inside a loop instead of set-based work across a collection.

Swarnil Singhai

Written by

Swarnil Singhai

Building Namaste Salesforce

Discussion