Classes
Apex Classes
Defining Apex Classes
Apex classes use public or global with methods and properties.
Introduction to Apex Classes
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform. Apex classes are the building blocks that allow developers to define the structure and behavior of objects within the Salesforce ecosystem.
This guide will delve into how to use public and global access modifiers with methods and properties in Apex classes.
Defining Apex Classes
In Apex, classes are defined using the class
keyword. A typical class definition includes access modifiers, class name, and a body that contains variables and methods.
Understanding Access Modifiers
Apex supports several access modifiers, but in this post, we focus on public and global. These modifiers determine the visibility of the class methods and properties.
- public: The class is visible across your application or namespace.
- global: The class is accessible by any Apex code that has access to the class, including code outside of your application or namespace.
Using Public Methods and Properties
Public methods and properties are accessible within your application. Use the public
keyword to define them. This is common for methods and properties that don't need to be exposed outside the application.
Using Global Methods and Properties
Global methods and properties are accessible from anywhere, even outside your application. Use the global
keyword when you want to expose elements to other applications or namespaces.
Best Practices for Access Modifiers
When deciding between public and global, consider the following best practices:
- Use public for most of your methods and properties to keep them within your application unless absolutely necessary to expose them.
- Reserve global for cases where integration with external applications or packages requires it.
Classes
- Classes
- Interfaces
- Inheritance
- Abstract Classes
- Enums
- Previous
- Security Basics
- Next
- Interfaces