Apex Variables Declaration 1737142598
Apex Variables Declaration 1737142598
What is a variable?
In Apex, a variable is a named storage location that holds a value which can be changed during the
execution of the program. Think of it as a container or a small box where you can store information.
This information can be a number, text, a date, or even a complex piece of data like a customer
record. In Apex, you define a variable by specifying its type (like Integer, String, or Date), giving it a
name, and optionally setting it to a specific value. Variables are fundamental in programming as they
allow you to store, modify, and retrieve data as your program runs, making it dynamic and flexible.
The declaration of variables in Apex is quite similar to java. Following is the syntax to declare an
apex variable:
1. Primitive Data Types: These include Integer , Long , Double , Decimal , String ,
2. sObject Types: These are specific to Salesforce and represent objects (like Account ,
4. Enums: These are a distinct type that consists of a fixed set of constants.
Declaring Variables
Variables in Apex must be declared with a specific data type. The syntax for declaring a variable is:
DataType variableName;
Integer count;
Initializing Variables
Variables can be initialized (assigned a value) when they are declared, or at any point later in the
program.
For example:
● Local Variables
● Instance Variables
● Static Variables
● Global Variables
● Constants
● Collection Variables
Local Variables
In Salesforce Apex, local variables are variables that are declared within a method and are
accessible only within that method. They are created when the method is called and exist until the
method has finished executing. Local variables help in performing operations within a method and
are not accessible outside of it, ensuring a level of encapsulation and preventing unintended
modifications from outside the method.
Here are three coding examples demonstrating the use of local variables in Apex:
// Adding numbers
Integer sum = a + b;
// Returning result
return sum;
}
Instance Variables
Instance variables, also known as member variables, are variables that are declared within a class
but outside any method, constructor, or block. In Salesforce Apex, these variables are used to store
data that is unique to each instance of the class.
3. Default Values: They are initialized to default values (e.g., null for objects, 0 for
Examples:
// Constructor
public Account(Decimal initialBalance) {
balance = initialBalance;
}
}
// Using the class
Account acc = new Account(500.00);
acc.deposit(200.00);
System.debug(acc.getBalance()); // Output: 700.00
Boolean result = acc.withdraw(100.00);
System.debug(result); // Output: true
Static Variables
In Salesforce Apex, the keyword static is used to define static methods and variables. Static
members (methods and variables) are associated with the class itself rather than any particular
instance of the class. This means that the static member is shared across all instances of the class.
Static Variables: They retain their value between method calls and are shared across all instances
of the class. They are ideal for maintaining a state that is common to all instances.
Static Methods: They can be called without creating an instance of the class. Static methods can
only directly access other static members.
Examples:
1. Static Variable:
Used to maintain a count of instances of a class or to share a common state across instances.
public Counter() {
count++;
}
In Salesforce Apex, global variables are not variables in the traditional sense, but rather, they are
references to context-specific information available in Visualforce pages or Apex classes. These
global variables allow you to access information about the current user, organization, or the current
page request, among others. They are typically used in Visualforce pages to personalize page
content according to the user’s context.
Here are three examples demonstrating the use of global variables in Salesforce Apex:
This example shows how you can use the UserInfo global variable to access information about the
In this example, the Organization object is used to retrieve information about the organization,
such as its name and primary contact. This isn’t a global variable per se, but it demonstrates how to
access global information about the Salesforce organization.
</apex:page>
This Visualforce page snippet uses global variables to personalize the content. $User accesses
information about the current user, $Profile accesses information about the user’s profile, and
While these examples showcase how to use global-like constructs in Salesforce to access specific
information about the user context, session, or organization, it’s important to note that Apex doesn’t
have global variables in the same sense as languages like JavaScript or Python. Instead, Salesforce
provides system classes and Visualforce global variables to access this kind of information.
Constants
Constants in Salesforce Apex are variables whose values are set at the time of declaration and
cannot be changed afterward. Constants are defined using the final keyword, and it is a common
practice to name constant variables in all uppercase letters to distinguish them from other variables.
They are typically used to store values that remain the same throughout the execution of the
program.
Here are three coding examples demonstrating the use of constants in Salesforce Apex:
Here, APP_NAME and MAX_LOGIN_ATTEMPTS are constants used for application configuration.
Their values are set once and used throughout the application.
error messages. They are used in the logError method to log specific error messages based on
These examples illustrate how constants are used in Apex to store values that do not change
throughout the execution of the program, providing a way to maintain clean, manageable, and less
error-prone code.
Collection Variables
In Salesforce Apex, collection variables are special types of variables that can store multiple values.
Apex provides three types of collection variables: Lists, Sets, and Maps.
1. Lists: An ordered collection of elements that are distinguished by their indices.
2. Sets: An unordered collection of elements that do not contain any duplicates.
3. Maps: A collection of key-value pairs where each unique key maps to a single value.
Example 1: List
Lists are ordered collections and can contain duplicate elements.
System.debug(size);
Example 2: Set
Sets are unordered collections of unique elements.
<p>Set numbers = new Set(); // Declare a new set of Integersnumbers.add(1); // Add
elements to the setnumbers.add(2);numbers.add(3);numbers.add(1); // Duplicate, will
not be added to the setSystem.debug(numbers); // Output: {1, 2, 3}</p>
Example 3: Map
Maps are collections of key-value pairs. Each unique key maps to a single value.