Apex #2 Classes and Objects
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.
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.
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.
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.
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."
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.
Our "Car" class now defines that every car object will have attributes for name and speed.
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:
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 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!
ncG1vNJzZmirkaGytLLOq5qeoJGYuLR60q6ZrKyRmLhvr86mZqlnkaWyuXmRZpqlmaOosrR5wKebZqeSn7KkwNI%3D