Apply Today

Creating your First Apex Class In Salesforce

apex classes development salesforce Sep 16, 2021
Zen In Tech
Creating your First Apex Class In Salesforce

Article Assumptions:

If you found this article, we assume that you are getting started with the Salesforce platform and the Apex language and want to dig deeper into customizing and automating the platform. For example, we will cover the fundamentals of what Classes are, how they work, and when to use them. We also assume you are just starting to write in the apex language and are familiar with the Developer Console. In addition, you may or may not understand programming concepts like OOP(Object Oriented Programming).

Our Method

You will first try to read and understand the code example. Then we will give you a further explanation of the code and what it means. This technique will activate your brain to decipher what you are reading; then, we will revisit this code later in the article to understand it better. Additionally, you can Play the Audio, read along, and allow your mind to process the information visually and auditorially.

In the Developer Console, create a new class by going to File → New → Apex Class and Name it Human, copy and paste our code, then save your work.

What is a Class?

An Apex Class is code that isolates and encapsulates information and functionality into a small package called an object. Objects are an attempt to model the natural world in a computer system. I would describe a Class as a design or blueprint (like DNA) which allows you to make more objects of the same type. Each class is a mini-program of its own.

What is an Object?

An Object is a product of a Class. Classes are similar to when a factory produces an item from a blueprint or template.

What is an Instance?

An Instance is an Object copied from a class template. It holds its Attributes in memory separate from different Instances of the same class. You can make almost an unlimited amount of object instance copies and can interact with them independently. For example, when you “Instantiate” an Object, you spawn a copy that will inherit all the attributes and methods of the Class.

Data Types

Data Types define what kind of data values an object can hold and what kind of operations it can perform. So essentially, your Class Name becomes the Data Type of the objects you create from it.

In the example above, we have a human class that will create objects from the Human Type or Data Type.

Data Types are classifications of objects that help the system determine the object's internal variables called attributes that store data and the operations it can perform on the data called methods. 

Objects can correspond to things found in the real world. So, for example, a graphics program will have objects such as circles, squares, menus. The object can inherit traits from Classes (Blueprints/Templates), and Classes themselves can inherit from other classes.

What is an Attribute/Property?

In coding, the Class is an abstract construct that holds the properties or attributes that describe and remember the characteristics of an object.

In line 2 of this example, we have Attributes/Properties of the Class, a fancy saying for a class variable that stores information.  Attributes/Properties are like nouns person, place, or a thing.

What is a Method?

The class also defines the functions of an object called Methods, which describes what an object does.

On line 9, we have our first method or function of a class. Processes are like verbs and describe the action of an object or what an object does.

When to use Classes?

There are only two coding structures you can use in apex to handle backend processes.

1. Triggers: Allow you to respond to database changes.

2. Classes: Control the logic, functionality, calculations, and data transformations with fine-grain system control in an encapsulated program.

Classes lay dormant until referenced. You can call Classes from other Classes, Triggers, Processes, Flows, API calls, front-end User Interface components, and from the Executed Anonymous window in the Developer Console.

In procedural programs, code is read from top to bottom executed in sequential order, but with classes, you can reference and execute code in different orders. In addition, while the code is running, you can call separate code that will block the execution code until it completes or runs asynchronously beside your code simultaneously.

How it works:

You create an object as an instance(Copy) of the Class, and it contains its unique memory. Each copy you create will contain its own unique space in the computer.

After you have saved your Class, use the Execute Anonymous window in the Developer Console.

From the Main Menu of the Developer Console, select DebugExecute Anonymous, then copy and execute the code below.

Execute Anonymous

The 3 Main Parts of a Class

Class Declaration

public class Human{     



Attribute Declaration

public String Name;


Method Declaration

 public void setName(String Name){ }


Review Code Example:


Breaking it down in English

  • Name: The Class Name is (Human)

  • Attributes: Name and Age

  • Methods: setName, setAge, Run, Jump, Climb

Class Deep Dive

Now that we have tapped into the surface of Classes, we must dig deeper to get a thorough understanding of the fine details.

Access Modifiers: Set the level of access to your class, attribute, or method

Examples: Global, Public or Private

Keywords: Reserved system words added or allow functionality in your code

Examples: Access Modifiers, Method Return Types

Methods: Operations or Actions of the Class

Method Name:


Return Types: Methods can return values of Different Data Types. In our example, we do not return a value, so the return type is void.


Parameters: Variable passed to methods



“this” Keyword: References the Class itself and is used to access the Attribute/Property of the Class



In Object-Oriented Programming (OOP), the Class is the fundamental construct used to create Objects in your programs. Understanding these concepts will have you well on your way in programming, and though the examples are related to Apex, these concepts are universal to Object-Oriented Programming. We have covered the basics of creating a Class file in Apex, however, we did not cover Inheritance, Polymorphism, Or Interfaces as these are advanced topics I will save for another article.

View our courses

Stay connected with news and updates!

Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.

We hate SPAM. We will never sell your information, for any reason.