Beginner 20 min read

Properties: get and set

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

  • Write a property with custom get and set logic
  • Explain why a property can be safer than a plain public field
  • Use a property to validate data on assignment

Prerequisites: "Static vs Instance Members"

A plain field has no protection

public class EventTicket {
    public Decimal price;
}

EventTicket ticket = new EventTicket();
ticket.price = -500; // perfectly legal, but nonsensical
  1. public class EventTicket { — a plain class with a plain field.
  2. public Decimal price; — nothing stops this field from holding a value that makes no business sense.
  3. } — closes the class.
  4. (blank line)
  5. EventTicket ticket = new EventTicket(); — creates a ticket.
  6. ticket.price = -500; — a negative ticket price shouldn't be allowed, but a plain field has no way to say so — this compiles and runs without complaint.

A property with validation in set

public class EventTicket {
    public Decimal price {
        get { return price; }
        set {
            if (value < 0) {
                price = 0;
            } else {
                price = value;
            }
        }
    }
}

EventTicket ticket = new EventTicket();
ticket.price = -500;
System.debug(ticket.price); // 0, not -500
  1. public class EventTicket { — the class.
  2. public Decimal price { — this is now a property, not a plain field — it looks the same from the outside but runs code behind the scenes.
  3. get { return price; } — runs whenever the property is read.
  4. set { — runs whenever the property is written.
  5. if (value < 0) { — value is a built-in keyword holding the incoming value being assigned.
  6. price = 0; — invalid input gets corrected before it's ever stored.
  7. } else { — otherwise…
  8. price = value; — …the value is stored as given.
  9. } — closes the else.
  10. } — closes set.
  11. } — closes the property.
  12. } — closes the class.
  13. (blank line)
  14. EventTicket ticket = new EventTicket(); — creates a ticket.
  15. ticket.price = -500; — the syntax looks identical to setting a plain field, but this now runs through the set block above.
  16. System.debug(ticket.price); — logs 0, not -500 — the invalid value was caught and corrected.

Auto-properties: the shorthand

public class EventTicket {
    public String eventName { get; set; }
}
  1. public class EventTicket { — the class.
  2. public String eventName { get; set; } — when no custom logic is needed, { get; set; } is shorthand for a property with default get/set behavior — functionally similar to a public field, but written in a way that leaves room to add real validation later without changing how callers use it.
  3. } — closes the class.

Exercise

Write a property called pageCount on a class Book that rejects negative values by storing 0 instead.

Show hint

set { if (value < 0) { pageCount = 0; } else { pageCount = value; } }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Properties: get and set Quiz

1. What does value refer to inside a property's set block?

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

A property looks like a plain field from the outside but can run real logic behind the scenes when read (get) or written (set) — most commonly used to validate or transform data.