Intro to Classes and Objects
Not Started

Data Data Data. All we talk about is data!

Software is built to manipulate data. As Apex developers, we are constantly reading and writing text, adding and subtracting numbers, validating, and passing around data.

We’ve already learned about primitive datatypes like String, Integer, and Boolean which give us incredible abilities to keep track of data in Apex. But so far we’ve let our variables run loose. If we’re not careful our software can get out of hand real quick.

Consider Apex that’s used to keep track of 4 employees. Based on what we’ve learned so far, our code might look like this:

String employeeName = 'Elaine'; Integer startYear = 2019; String department = 'Engineering'; Boolean isCurrentEmployee = true; String employeeName2 = 'George'; Integer startYear2 = 2023; String department2 = 'Sales'; Boolean isCurrentEmployee2 = true; String employeeName3 = 'Jerry'; Integer startYear3 = 2017; String department3 = 'Buiz-Ops'; Boolean isCurrentEmployee3 = true; String employeeName4 = 'Kraemer'; Integer startYear4 = 2021; String department4 = 'Human Resources'; Boolean isCurrentEmployee4 = true;

This code is very tedious to read. We shouldn’t have to remember that employeeName3 is “Jerry” and all variables ending with 3 belong to Jerry.

We already recognize logical groupings. So let’s formalize it by grouping this information together into an entity. The entity can represent a single Employee by having the attributes of an employee like name, start year, department, etc. This would make our software more cohesive. This is where a Class comes in. A class allows developers to create their own custom datatypes and use them just like primitive datatypes.

The class will act as a blueprint. It will instruct Apex on how to create the new custom datatype, called an Object, by defining properties and behavior. Let’s create a class called Employee and use it to encapsulate the employee variables as properties:

public class Employee { String name; Integer startYear; String department; Boolean isCurrentEmployee; }

Not that scary, right? We now have a Class Definition. We’ve defined the class using the public class keywords and assigned the nameEmployee.

The { and } indicate the scope of the class indicating where the class begins and ends.

Inside the braces, we’ve defined the properties of an Employee. You may see these types of variables referred to as properties, attributes, fields, or member variables. The Apex Developer Guide refers to them as member variables, so we will too.

Notice how the content inside the class is indented with a tab. This is not mandatory, but it’s a common styling convention developers follow to make the code easier to read. We’d suggest you follow the same style.

There are mandatory naming restrictions for classes. Thankfully, they have the same restrictions as variable names. Class names can not start with a number and they cannot contain special characters other than an underscore.

Class names traditionally adhere to UpperCamelCase casing. Recall that Apex is case insensitive, so Employee and employee are technically the same thing.

We’ll dive deep into the correct syntax in the next lesson. For now, let’s create your first class. Replace the text REPLACE_ME with Student to create a Student class. Uncomment the line to declare a member variable of type String named firstName.

Challenge

We’ll dive deep into the correct syntax in the next lesson. For now, let’s create your first class. Replace the text REPLACE_ME with Student to create a Student class. Declare a member variable of type String named firstName. You don’t need to initialize it.