Beginner 20 min read

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"
  1. public class EventTicket { — the blueprint.
  2. public String eventName; — a field.
  3. public Decimal price; — another field.
  4. (blank line)
  5. public String describe() { — a method living inside the class, alongside the fields.
  6. return eventName + ' — R' + price; — eventName and price here refer to that specific object's values — the same method, called on a different EventTicket object, would use that object's own data instead.
  7. } — closes describe().
  8. } — closes the class.
  9. (blank line)
  10. EventTicket ticket = new EventTicket(); — creates an object.
  11. ticket.eventName = 'Summer Music Festival'; — sets its name.
  12. ticket.price = 350; — sets its price.
  13. (blank line)
  14. 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"
  1. EventTicket vipTicket = new EventTicket(); — the first ticket.
  2. vipTicket.eventName = 'Tech Conference'; — same event.
  3. vipTicket.price = 1200; — a higher price.
  4. (blank line)
  5. EventTicket generalTicket = new EventTicket(); — a second, separate ticket.
  6. generalTicket.eventName = 'Tech Conference'; — same event name.
  7. generalTicket.price = 450; — a different, lower price.
  8. (blank line)
  9. System.debug(vipTicket.describe()); — logs "Tech Conference — R1200".
  10. 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';

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Creating Your First Class Quiz

1. When a method inside a class refers to one of the class's fields, whose value does it use?

Log in to submit the quiz and save your score.

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.