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
public class EventTicket {— the blueprint.public String eventName;— an instance field — one copy per object.public Decimal price;— another instance field.- (blank line)
public static Integer ticketsCreated = 0;—staticmeans there is only one copy of this field total, shared by everyEventTicketobject ever created — not one per ticket.- (blank line)
public EventTicket(String eventName, Decimal price) {— the constructor.this.eventName = eventName;— sets this object's name.this.price = price;— sets this object's price.ticketsCreated++;— increments the shared counter every time any ticket is created.}— closes the constructor.}— closes the class.- (blank line)
EventTicket t1 = new EventTicket('Conference', 1200);— creates the first ticket, incrementingticketsCreatedto1.EventTicket t2 = new EventTicket('Workshop', 400);— creates a second, incrementing it to2.- (blank line)
System.debug(EventTicket.ticketsCreated);— accessed through the class name, not through any specific object — logs2.
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
public class TaxCalculator {— a class with no fields at all, just a calculation.public static Decimal applyVat(Decimal amount) {— astaticmethod belongs to the class itself, not to any object.return amount * 1.15;— the calculation.}— closes the method.}— closes the class.- (blank line)
Decimal total = TaxCalculator.applyVat(100);— called directly on the class name, without ever creating aTaxCalculatorobject — 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.
Static vs Instance Members Quiz
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.