Class and Object in Apex
Apex Classes and Objects in Salesforce Apex
In Salesforce Apex, classes and objects are foundational concepts that allow developers to model real-world entities and their behaviors in code. Apex is an object-oriented programming language, which means that the core building blocks of any Apex program are objects, and these objects are defined by classes.
Understanding Apex Classes
Classes in Apex serve as blueprints or templates for creating objects. They encapsulate data for the object (through variables) and the behavior of the object (through methods). A class can have attributes (variables) that represent the state of an object and methods that define what the object can do.
Key Components of an Apex Class:
- Attributes (Variables): These are the properties of the class. They define the characteristics or data associated with objects of the class.
- Methods: These define the actions or behaviors that objects of the class can perform. Methods can modify the object’s state or perform calculations.
- Constructors: Special methods used to initialize objects when they are created.
- Access Modifiers: Define the visibility of the class and its methods/variables (e.g.,
public
,private
,global
).
Example: Creating an Apex Class
Let’s consider a scenario where we need to model a Book in a library system.
public class Book {
// Attributes (Variables)
public String title;
public String author;
public Integer pages;
public Boolean isCheckedOut;
// Constructor
public Book(String title, String author, Integer pages) {
this.title = title;
this.author = author;
this.pages = pages;
this.isCheckedOut = false; // By default, the book is not checked out
}
// Method to check out the book
public void checkOut() {
if (!this.isCheckedOut) {
this.isCheckedOut = true;
System.debug(this.title + ' has been checked out.');
} else {
System.debug(this.title + ' is already checked out.');
}
}
// Method to return the book
public void returnBook() {
if (this.isCheckedOut) {
this.isCheckedOut = false;
System.debug(this.title + ' has been returned.');
} else {
System.debug(this.title + ' was not checked out.');
}
}
}
Explanation:
- Attributes: The class
Book
has four attributes:title
,author
,pages
, andisCheckedOut
. These attributes hold the state of a Book object. - Constructor: The constructor
Book(String title, String author, Integer pages)
is used to initialize the attributes when a newBook
object is created. For example, when you create a newBook
, you provide a title, author, and number of pages. - Methods: The
checkOut()
method changes the state of theisCheckedOut
attribute totrue
and prints a message. ThereturnBook()
method changes the state ofisCheckedOut
back tofalse
and prints a message.
Understanding Apex Objects
An object in Apex is an instance of a class. While a class defines the structure and behavior, an object represents a specific entity with the defined structure and behavior.
Example: Creating and Using Apex Objects
Using the Book
class, we can create multiple book objects:
// Creating objects (instances of the class Book)
Book book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 180);
Book book2 = new Book('1984', 'George Orwell', 328);
// Using methods on the objects
book1.checkOut(); // Output: The Great Gatsby has been checked out.
book2.checkOut(); // Output: 1984 has been checked out.
book1.returnBook(); // Output: The Great Gatsby has been returned.
book2.returnBook(); // Output: 1984 has been returned.
Explanation:
- Creating Objects:
Book book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 180);
creates an instance of theBook
class. Here,book1
is an object of theBook
class. - Using Methods: You can call methods on objects. For example,
book1.checkOut();
changes theisCheckedOut
attribute of thebook1
object and prints a message.