Beginner 25 min read

Static vs Instance Members

By the end of this lesson, you'll be able to:

  • Distinguish a static field/method from an instance field/method
  • Explain that static members belong to the class itself, not to any one object
  • Call a static method without creating an object

Prerequisites: "Constructors"

Instance members: one per object

Every field and method you've written so far — eventName, price, describe() — is an instance member. Each EventTicket object gets its own separate copy of eventName and price; that's why two tickets can have different prices at once.

Static members: one per class, shared by everyone

public class EventTicket {
    public String eventName;
    public Decimal price;

    public static Integer ticketsCreated = 0;

    public EventTicket(String eventName, Decimal price) {
        this.eventName = eventName;
        this.price = price;
        ticketsCreated++;
    }
}

EventTicket t1 = new EventTicket('Conference', 1200);
EventTicket t2 = new EventTicket('Workshop', 400);

System.debug(EventTicket.ticketsCreated); // 2
  1. public class EventTicket { — the blueprint.
  2. public String eventName; — an instance field — one copy per object.
  3. public Decimal price; — another instance field.
  4. (blank line)
  5. public static Integer ticketsCreated = 0; — static means there is only one copy of this field total, shared by every EventTicket object ever created — not one per ticket.
  6. (blank line)
  7. public EventTicket(String eventName, Decimal price) { — the constructor.
  8. this.eventName = eventName; — sets this object's name.
  9. this.price = price; — sets this object's price.
  10. ticketsCreated++; — increments the shared counter every time any ticket is created.
  11. } — closes the constructor.
  12. } — closes the class.
  13. (blank line)
  14. EventTicket t1 = new EventTicket('Conference', 1200); — creates the first ticket, incrementing ticketsCreated to 1.
  15. EventTicket t2 = new EventTicket('Workshop', 400); — creates a second, incrementing it to 2.
  16. (blank line)
  17. System.debug(EventTicket.ticketsCreated); — accessed through the class name, not through any specific object — logs 2.

Static methods: no object required

public class TaxCalculator {
    public static Decimal applyVat(Decimal amount) {
        return amount * 1.15;
    }
}

Decimal total = TaxCalculator.applyVat(100); // 115, no "new" needed
  1. public class TaxCalculator { — a class with no fields at all, just a calculation.
  2. public static Decimal applyVat(Decimal amount) { — a static method belongs to the class itself, not to any object.
  3. return amount * 1.15; — the calculation.
  4. } — closes the method.
  5. } — closes the class.
  6. (blank line)
  7. Decimal total = TaxCalculator.applyVat(100); — called directly on the class name, without ever creating a TaxCalculator object — fitting for a calculation that doesn't depend on any one object's specific data.

Exercise

Add a static field totalPages to the Book class that accumulates the pageCount of every Book created, updated inside the constructor.

Show hint

public static Integer totalPages = 0; then totalPages += pageCount; inside the constructor.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Static vs Instance Members Quiz

1. How many copies of a static field exist across all objects of a class?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Instance members belong to one specific object; static members belong to the class itself and are shared across every object — and can even be used without creating an object at all.