What is Classes and Objects in Apex?
Classes and Objects in Apex. Simply put, a class in Apex is a recipe for creating objects. Obviously, speaking of people as “objects” is not good. But in object-oriented programming, everything is an object, even people!

Before we begin, an important note: An object in Apex is not the same as an object in the Salesforce UI. Even though we’re using the same word, these are two completely different things.
An object in Salesforce is something that you create in the UI or a standard Salesforce object. Meanwhile, an object in Apex exists only while you run the code. As such, a class is kind of like a Salesforce object with actions. When you create a custom object in Salesforce, you’re making a recipe for how to create records.

First class
To create a class, navigate to the Developer Console, click File → New → Apex Class and type in the name of your class (e.g. “Person”).

Salesforce will automatically create the basic class for you:

“public” and “class” are keywords. “Public” just means that your class is available for all other applications within your org. “class” is a keyword that allows the Apex engine to identify that it’s a class.

Within the brackets, you can write your actual class logic. Keep in mind that you can only write a few things within them:
- Comments
- Variables
- Methods
- Constructors
- Other classes (called inner classes)
Class variables
A person can definitely have a name and an age. Let’s create corresponding variables:
public class Person {
public String name;
public Integer age;
}
This class is quite abstract—our person doesn’t have a name or age. With this recipe, however, we can create very specific “person” objects. Also note that these are Apex objects. Navigate to Developer Console → Debug → Anonymous Window and write the following code:
Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
Person person2 = new Person();
person2.name = 'John';
person2.age = 18;
We just created two Apex objects with the “Person” class. These are very specific people that have names and ages. The syntax for setting the value of a variable is quite straightforward:
classVariableName.variable = value;
We can set and get the value with the same sort-syntax:
Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
System.debug('Name of the first person: ' + person1.name); // Alex
System.debug('Age of the first person: ' + person1.age); // 36
A class is a data type
You’ve probably noticed that creating person variables is similar to creating string variables. Check it out:
String name = 'Alex';
Person person1 = new Person();
That’s because your classes are data types that you create on your own. Moreover, all data types in Apex are classes. Every String variable uses a “String” class, but Salesforce developers already wrote this class for you.
There is one difference that you can see in the example above: the “new” keyword. Let’s discuss what it does.
The “new” keyword
With the “new” keyword, you’re signaling to the Apex engine that you want to create an Apex object.

Apex will then immediately allocate an actual physical space somewhere on the server for your object.

So why not create String or Integer objects with the “new” keyword? In some languages, we do! For example, in Java, you can create a string variable like this:
String name = new String('Alex');
And this syntax makes sense for all primitive types. However, Strings and Integers are used so often that Salesforce developers decided to simplify the syntax. Now, we can only use the shortcut version for creating string variables. In the end, there’s no difference at all. We’re still creating objects from classes!
Apex objects
An Apex object is nothing more than a collection of fields. You can see the object if you print out any object variable with “System.debug();.”
Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
System.debug(person1);
For example, for the “Person” class, the object contains the name and age of this specific person:

4 Comments