What Are Classes and Objects?
What is a Class?
In Salesforce Apex, a class is like a recipe or a blueprint. It defines how to create objects, just as a recipe instructs you on how to bake a cake. Think of a class as the set of instructions for making something, with all the ingredients and steps clearly defined.
What is an Object?
An object, on the other hand, is an actual, tangible thing created from a class. It's like the cake you bake using the recipe. Every cake you make follows the same recipe, but each cake is unique in its own way.
A Real-World Example
Let's take a practical example to understand these concepts better. Imagine you're building a system to manage cars. We'll create a class to define what a "car" is and then use that class to create individual car objects.
Step 1: Creating a Class
Opening the Developer Console
First, you need to open the Developer Console, which is the tool where you write and run your Apex code. You can find it in your Salesforce environment.
Creating a New Apex Class
In the Developer Console, click on the
File
menu.Select
New
.Choose
Apex Class
.
Now, give your class a name. For this example, let's call it "Car."
Step 2: Defining Class Variables
In our "Car" class, we'll define the attributes that a car might have. These attributes are like the characteristics of a car, such as its name and speed.
Here's how you define class variables in Apex:
public class Car {
public String name;
public Integer speed;
}
The keyword public
indicates that these variables can be accessed from other parts of your code.
String
andInteger
are data types that represent text (like name) and whole numbers (like speed).
Our "Car" class now defines that every car object will have attributes for name and speed.
Step 3: Creating and Using Car Objects
Now, let's create car objects based on our "Car" class. These objects will represent individual cars with their own specific name and speed.
Here's how we do it:
Car myCar = new Car();
myCar.name = 'Bmw';
myCar.speed = 100;
Car anotherCar = new Car();
anotherCar.name = 'Audi';
anotherCar.speed = 120;
Here's what we did:
Car myCar = new Car();
This line creates an instance of the "Car" class and assigns it to the variable myCar.
new Car() invokes the constructor of the "Car" class, which initializes the object.
Essentially, it creates a new car object, and myCar is now a reference to this object.
myCar.name = 'Bmw';
Here, we're setting the
name
attribute of the myCar object to 'Bmw'.This is an example of how you can assign values to the properties or attributes of an object.
myCar.speed = 100;
Similarly, we're setting the
speed
attribute of the myCar object to 100.You can set multiple attributes or properties of an object like this to define its characteristics.
Car anotherCar = new Car();
This line is analogous to the first line, but it creates a new car object and assigns it to the variable anotherCar.
Now, you have two car objects: myCar and anotherCar.
anotherCar.name = 'Audi';
Here, we're setting the
name
attribute of the anotherCar object to 'Audi'.Just like with the myCar object, we can set the attributes of anotherCar.
anotherCar.speed = 120;
This line sets the
speed
attribute of the anotherCar object to 120.
The Simplicity of Apex Objects
You may be surprised to learn that in Apex, classes and objects are closely related to the variables you use every day. In fact, every data type in Apex is treated as a class, and even the most basic data types like text and numbers are considered objects.
For example, just as you create a car object like this:
Car myCar = new Car();
You can create a text object (like a name) or a number object (like an age) in a similar way:
String myName = new String('Alice');
In this code, myName is an instance of the String
class. You're essentially creating a string object named myName with the content 'Alice'. You might wonder, why use the new
keyword here? In Apex, the new
keyword signals the creation of a new instance of a class. While it may seem a bit redundant for basic data types like strings, it maintains consistency in how all data types are handled in Apex.
Integer myAge = new Integer(30);
Similar to the previous example, here we're creating an integer object named myAge with the value 30. Again, the new
keyword is used to indicate that you're creating a new instance of the Integer
class.
In Summary
In this beginner's guide to classes and objects in Salesforce Apex, you've learned the basics using a simple example. A class is like a blueprint that defines the properties and behavior of objects, while an object represents a real instance of that class with specific properties.
This understanding will serve as the foundation for your journey into Salesforce development. Lets talk about methods, if/else statements in the next post. Keep exploring, stay curious, and enjoy your coding adventure!