Creating Your First Class
By the end of this lesson, you'll be able to:
- Write a complete class with fields and a method together
- Call a method on a specific object using dot notation
- Explain how a method inside a class can use that object's own fields
Prerequisites: "What Is an Object?"
Fields and methods together
public class EventTicket {
public String eventName;
public Decimal price;
public String describe() {
return eventName + ' — R' + price;
}
}
EventTicket ticket = new EventTicket();
ticket.eventName = 'Summer Music Festival';
ticket.price = 350;
System.debug(ticket.describe()); // "Summer Music Festival — R350"
public class EventTicket {— the blueprint.public String eventName;— a field.public Decimal price;— another field.- (blank line)
public String describe() {— a method living inside the class, alongside the fields.return eventName + ' — R' + price;—eventNameandpricehere refer to that specific object's values — the same method, called on a differentEventTicketobject, would use that object's own data instead.}— closesdescribe().}— closes the class.- (blank line)
EventTicket ticket = new EventTicket();— creates an object.ticket.eventName = 'Summer Music Festival';— sets its name.ticket.price = 350;— sets its price.- (blank line)
System.debug(ticket.describe());— calls the method on this object, logging"Summer Music Festival — R350".
A real business example: Event Management
EventTicket vipTicket = new EventTicket();
vipTicket.eventName = 'Tech Conference';
vipTicket.price = 1200;
EventTicket generalTicket = new EventTicket();
generalTicket.eventName = 'Tech Conference';
generalTicket.price = 450;
System.debug(vipTicket.describe()); // "Tech Conference — R1200"
System.debug(generalTicket.describe()); // "Tech Conference — R450"
EventTicket vipTicket = new EventTicket();— the first ticket.vipTicket.eventName = 'Tech Conference';— same event.vipTicket.price = 1200;— a higher price.- (blank line)
EventTicket generalTicket = new EventTicket();— a second, separate ticket.generalTicket.eventName = 'Tech Conference';— same event name.generalTicket.price = 450;— a different, lower price.- (blank line)
System.debug(vipTicket.describe());— logs"Tech Conference — R1200".System.debug(generalTicket.describe());— logs"Tech Conference — R450".
One describe() method, reused by every ticket object — each call naturally reflects that specific ticket's own price, because the method reads its own object's fields.
Exercise
Write a class named Book with fields title (String) and pageCount (Integer), and a method summary() that returns a String combining both. Create one object and debug its summary.
Show hint
return title + ' has ' + pageCount + ' pages';
Creating Your First Class Quiz
Leaderboard
- 1. Monde Masiza 1/1
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
Classes combine data (fields) and behavior (methods) in one place — this lesson writes a complete class where a method uses the object's own field values.