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

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.

FoundationsBeginner9 min readJul 10, 2026
Analogies

Anatomy of an Apex Class

An Apex class is a template that defines properties (fields) and behavior (methods). A class declaration consists of an access modifier, the class keyword, a name in PascalCase, and a body enclosed in curly braces. Once a class exists, you create individual objects, or instances, of it using the new keyword, each with its own independent state.

🏏

Cricket analogy: Like a coaching manual template defining every player's role and drills, from which each actual player, an instance, follows the same blueprint but ends up with their own individual stats.

Writing a Simple Class with Methods

A method has a return type (or void if it returns nothing), a name, a parameter list in parentheses, and a body. Instance methods operate on a specific object's data and are called through an instance, while static methods belong to the class itself and are called directly on the class name without ever creating an instance.

🏏

Cricket analogy: An instance method is like a specific player's individual technique, say Kohli's cover drive, callable only through him, while a static method is like a universal law of cricket such as the LBW rule that applies without needing any specific player.

Access Modifiers and Static vs Instance

Access modifiers such as public, private, protected, and global control visibility; private is the implicit default if none is specified for a class member. Static members belong to the class itself and are shared across every instance, while instance members belong separately to each individual object created from the class.

🏏

Cricket analogy: Private is like a team's internal training drills hidden from opponents, while public is like the published team sheet everyone can see; a static counter is like a tournament's shared total-runs tally across all matches, while an instance field is like each individual match's own scorecard.

Testing Your Class

Apex requires dedicated test classes, annotated with @isTest, and Salesforce enforces a minimum of 75% code coverage org-wide before Apex can be deployed to a production org. Test methods typically wrap the code under test in Test.startTest()/Test.stopTest(), which resets governor limits and forces asynchronous work to finish, then use System.assertEquals() to verify the actual outcome matches what was expected.

🏏

Cricket analogy: Like a player needing to pass a fitness test, the equivalent of asserting expected conditions, before being cleared to play in an official match, with a minimum fitness threshold required by the board just as 75% coverage is required by the platform.

apex
public class GreetingService {
    public static String getGreeting(String name) {
        if (name == null) {
            return 'Hello, Guest!';
        }
        return 'Hello, ' + name + '!';
    }
}

@isTest
private class GreetingServiceTest {
    @isTest
    static void testGetGreetingWithName() {
        Test.startTest();
        String result = GreetingService.getGreeting('Trailblazer');
        Test.stopTest();
        System.assertEquals('Hello, Trailblazer!', result, 'Greeting should include the name');
    }
}

Salesforce requires at least 75% overall code coverage org-wide, with every trigger having some coverage, before Apex metadata can be deployed to a production org - though aiming for meaningful assertions, not just hitting the number, is the real goal.

Tests that only call a method without any System.assert statements provide code coverage but no real verification. A class can reach 100% coverage and still contain bugs if the tests never actually check the output values.

  • An Apex class is declared with an access modifier, the class keyword, a PascalCase name, and a body in braces.
  • Instances are created using the new keyword; instance methods operate on that specific instance's state.
  • Static methods and variables belong to the class itself and are shared across all instances.
  • Access modifiers (private, public, protected, global) control visibility; private is the default for class members.
  • Test classes are annotated with @isTest and are required for deployment validation.
  • Test.startTest()/Test.stopTest() reset governor limits and force async operations to complete within the test.
  • A minimum of 75% code coverage (org-wide) is required before deploying Apex to production.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ApexSalesforceStudyNotes#YourFirstApexClass#Apex#Class#Anatomy#Writing#OOP#StudyNotes#SkillVeris