• Lessons Home
Topics
Community
  1. Lessons
  2. Object-Oriented Apex
  3. Classes and Objects
  4. Defining Classes

      Defining Classes

      Defining Classes

      Every exercise so far has been a single method working on values like Integer and String. Real programs model things: an order, a support ticket, a bank account. A class lets you describe a new kind of thing once, and an object is one real thing built from that description.

      A Class Is a Blueprint

      A class groups related data, called fields, with the code that works on that data, called methods:

      public class Dog {
      	public String name;
      	public Integer age;
      
      	public String describe() {
      		return name + ' is ' + age + ' years old';
      	}
      }
      

      Notice that describe() has no static keyword. It is an instance method, so it runs on one particular object and can read the fields of that object directly by name.

      In a Salesforce org each class usually lives in its own file. In the exercises, you write the class right in the editor.

      Creating Objects with new

      The class itself holds no data. You create an object from it with the new keyword, then use a dot to reach its fields and methods:

      Dog rex = new Dog();
      rex.name = 'Rex';
      rex.age = 3;
      System.debug(rex.describe()); // Rex is 3 years old
      

      The class you define becomes a brand new type, so the variable is declared as Dog rex, just like Integer count.

      Each Object Has Its Own Fields

      Every object gets its own copy of the fields. Changing one object never changes another:

      Dog luna = new Dog();
      luna.name = 'Luna';
      luna.age = 5;
      rex.age = 4;
      System.debug(luna.age); // still 5
      

      Methods can change fields too. A method like public void haveBirthday() { age = age + 1; } updates only the object you call it on.

      Common Mistakes

      A field you never set starts out as null, even an Integer. Calling haveBirthday() on a new Dog whose age was never set throws a null pointer exception, so give a field a value before you do math with it.

      Why This Matters

      Almost all Salesforce code is organized into classes. Triggers hand their work to handler classes, and service classes group the logic for one job. Knowing how to define a class and create objects from it is the foundation for everything else in object-oriented Apex.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      How It Works

      • • Write your solution in the code editor
      • • Connect your Salesforce org to test
      • • Submit to check if your solution passes
      • • Use hints if you get stuck

      Note

      Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In