Skip to main content

SF-0303 · Concept · Easy

What is an apex class or a class?

✓ Verified by Vikas Singhal · Last reviewed 5/17/2026 · Updated for Spring '26

An Apex class is a template that defines the structure and behaviour of objects. Like Java — the language Apex is modelled on — a class bundles together fields (state), methods (behaviour), constructors (initialization logic), inner classes, and enums into a single named, reusable unit. Almost every line of production Apex lives inside a class.

Minimal anatomy

public with sharing class AccountService {

    // Field
    private static final Integer MAX_RESULTS = 200;

    // Constructor
    public AccountService() {
        // Initialization
    }

    // Static method
    public static List<Account> getActive(Integer limit) {
        return [
            SELECT Id, Name, Industry
            FROM Account
            WHERE Active__c = true
            LIMIT :limit
        ];
    }

    // Instance method
    public Boolean isHighValue(Account a) {
        return a.AnnualRevenue > 10000000;
    }
}

That single file contains everything you need to instantiate the class and call methods on it.

What goes into a class

A complete Apex class can contain:

MemberPurposeExample
FieldsState held by an instance or the classprivate String name;
ConstantsCompile-time constantsstatic final Integer MAX = 100;
ConstructorsInitialize a new instancepublic AccountService() { ... }
MethodsBehaviour — static or instancepublic Boolean isValid() { ... }
PropertiesField with getter/setter syntaxpublic Integer count { get; set; }
Inner classesNested classes scoped to the outer classpublic class WrapperRow { ... }
EnumsNamed value setspublic enum Status { ACTIVE, INACTIVE }

Modifiers on the class itself

Every class declaration carries modifiers that affect visibility and behaviour:

public with sharing virtual class AccountService extends BaseService implements Cacheable {
   ...
}

The pieces:

  • public — visible to other Apex in the same namespace. Other options: private (only valid for inner classes), global (visible across namespaces — required for managed packages and @RestResource).
  • with sharing — runs in user mode, respects the running user’s sharing rules. Alternatives: without sharing (system mode, ignores sharing) and inherited sharing (uses the caller’s mode).
  • virtual — allows subclasses to extend and override methods. Default classes are sealed.
  • abstract — class cannot be instantiated directly; subclasses must implement abstract methods.
  • extends / implements — inheritance from a parent class or interfaces.

Creating instances

AccountService svc = new AccountService();
Boolean ok = svc.isHighValue(someAccount);

// Static methods skip the instance
List<Account> active = AccountService.getActive(50);

If a class has only static methods (a utility class), there’s no reason to instantiate it. Many teams enforce this by giving the class a private constructor.

public class StringUtil {
    // Prevents instantiation — this is a utility class
    private StringUtil() {}

    public static String slugify(String input) { /* ... */ }
}

File layout

Each top-level Apex class lives in its own .cls file alongside a .cls-meta.xml metadata file. The class name in the file must match the file name. Inner classes live inside their containing file and do not get their own files.

Inner classes — the most common pattern

public class OrderProcessor {
    // Inner class for structured returns from service methods
    public class OrderSummary {
        @AuraEnabled public Decimal total;
        @AuraEnabled public Integer itemCount;
        @AuraEnabled public String currencyCode;
    }

    public static OrderSummary summarize(Id orderId) {
        OrderSummary s = new OrderSummary();
        // ... compute ...
        return s;
    }
}

Inner classes are how Apex passes structured data to Lightning Web Components, batch handlers, and callouts. They’re also how you keep helper types from polluting your namespace.

What interviewers are really looking for

The interviewer is checking that you know Apex is object-oriented in the same shape as Java — classes, instances, modifiers, inheritance, interfaces. Quick wins: name the access modifiers (public/private/global), the sharing modifiers (with/without/inherited sharing), and the inheritance keywords (virtual, abstract, extends, implements). Mention that inner classes are the standard way to shape data for LWC, and you’ve signalled production fluency.

Verified against: Apex Developer Guide — Classes. Last reviewed 2026-05-17 for Spring ‘26 release.