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
public class EventTicket {— a plain class with a plain field.public Decimal price;— nothing stops this field from holding a value that makes no business sense.}— closes the class.- (blank line)
EventTicket ticket = new EventTicket();— creates a ticket.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
public class EventTicket {— the class.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.get { return price; }— runs whenever the property is read.set {— runs whenever the property is written.if (value < 0) {—valueis a built-in keyword holding the incoming value being assigned.price = 0;— invalid input gets corrected before it's ever stored.} else {— otherwise…price = value;— …the value is stored as given.}— closes theelse.}— closesset.}— closes the property.}— closes the class.- (blank line)
EventTicket ticket = new EventTicket();— creates a ticket.ticket.price = -500;— the syntax looks identical to setting a plain field, but this now runs through thesetblock above.System.debug(ticket.price);— logs0, not-500— the invalid value was caught and corrected.
Auto-properties: the shorthand
public class EventTicket {
public String eventName { get; set; }
}
public class EventTicket {— the class.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.}— 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; } }
Properties: get and set 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
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.