Classes and Objects in Apex

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!

Classes and Objects in Apex
Classes and Objects in Apex

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.

basics of apex merged 7

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”).

basics of apex merged 8

Salesforce will automatically create the basic class for you:

image 1

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.

basics of apex merged 9

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(); = 'Alex';
person1.age = 36;
Person person2 = new Person(); = '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(); = 'Alex';
person1.age = 36;
System.debug('Name of the first person: ' +; // 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.

basics of apex merged 10

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

basics of apex merged 11

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(); = 'Alex';
person1.age = 36;

For example, for the “Person” class, the object contains the name and age of this specific person:

image 2
<h4 class="item-title">admin</h4>


Related Posts

blog images 12

SOQL in Apex

blog images 11

Loops in Apex

blog images 10

Lists in Apex


  • Fabrice Senior Developpeur Salesforce CHALLIER March 22, 2023

    I just a big fan of your site / design / ideas.
    I was wondering, why don’t you say that variables in apex can be compare to fields in the Salesforce object.
    I’m used to do this comparison when training newbies

    I may provide comments in the future if you don’t mind to highlights my ideas.
    Not sure all will be good so forgive me 🙂

    • User Avatar
      admin April 4, 2023

      Hi, thank you for your kind words! The reason is, because they are not the same. You can compare variables with a URL that point to the Salesforce record, but not with the field. If I understand you correctly 🙂 I also just had a post on LinkedIn about it 😀

  • Anna Szabo May 28, 2023

    Thank you for not only your helpful content but also for memorable illustrations. This blog is priceless, and I just filmed a video with the resources I’m using to prepare for the Salesforce PD1 exam, in which YOU are included on my YouTube channel. This blog collection is a blessing!

    • User Avatar
      Ramkrishna Hirani August 12, 2023

      Thank you it’s very helpful content

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe for news about new courses or discounts.

No spam, we promise.

    © 2023 All Rights Reserved by site