3.1 Define a custom object using class

In this chapter, we are going to cover the very basics of custom class, object and method. Object-Oriented programming is a complicated concept and we will cover it in more details later. In this chapter, I will show you about how to create and use a custom class, and how to utilize methods to write dry code.

The concept

In last chapter, we have introduced using variables to store data. We can use decimals to store numbers and strings to store texts. In some simple cases, this would be good enough. But what if we want to store some data for some more complicated objects? Like, a dog?

Let’s say for each dog we need to know its name, age, gender, breed and whether it is still a puppy. Sure, we can use independent variables to define them. But the drawbacks are very obvious:

  1. Those variables are not grouped together. So it can be hard to manipulate.
  2. The code can be quite messy if we use this strategy to define multiple dogs.

In order to resolve this issue, we have class to help us. Below is the sample code for creating a class.

public class Dog
{
    public Integer age;
    public String name;
    public String gender;
    public String breed;
    public Boolean isPuppy;
}

You can put the class definition code in anonymous block. But it is always a good idea to put the class definition in a separate class. So let’s do it.

In your developer console, click File>New>Apex Class, in the prompting dialog, enter the class name. In this case, dog. Replace the code with our code above. Click save. You should have no errors.

Then go back to your anonymous block by clicking ctrl + e or cmd + e if you have using Mac. Type in the following code:

Dog d = new Dog();
d.age = 20;
d.name = 'Happy';
d.gender = 'male';
d.breed = 'Golden Retriever';
d.isPuppy = false;

System.debug(d.breed);

That is pretty much the code. Execute it and see the result. How do you feel about it?

A bit explanation

public is the access modifier for class, variables and methods. It defines whether this class/variable/method can be accessed by public or just within this section of code. We will talk about access modifiers in more detail later in this chapter. Top-tier class can only be public or global.

class is the keyword to tell the compiler this is the definition of a class.

Everything inside the {} is part of the class definition.

To create a new dog object instance, we can use dog d = new dog(); d is also a variable. Just like any other variables like integer or string. The only difference is its type is dog.

To retrieve a public variable inside the dog class, we can use the ObjectName.VariableName grammar, in this case, it is d.name, d.gender, etc.

Exercise

Define a Person class. A person should have a first name, a last name, a gender, an age, and whether it is an adult. Create an instance of that Person class

Next Post

3.2 Use methods

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

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