Methods in Apex:
Methods are blocks of code that perform a specific task and are defined within classes in Apex. They encapsulate functionality and can be invoked from other parts of the code. Let's explore the key features of methods in Apex:
Method Declaration:
public class MyClass {
public void myMethod() {
// Code implementation goes here
}
}
Here, public
is an access modifier indicating that the method is accessible outside the class. void
signifies that the method doesn't return any value.
Method Parameters:
Methods can accept parameters, enabling them to receive input data.
public class MyClass {
public void addNumbers(Integer num1, Integer num2) {
Integer sum = num1 + num2;
System.debug('Sum: ' + sum);
}
}
In this example, the addNumbers
method takes two Integer parameters (num1
and num2
) and calculates their sum.
Return Types:
Methods can also return values of a specified data type.
public class MyClass {
public Integer multiplyNumbers(Integer num1, Integer num2) {
return num1 * num2;
}
}
The multiplyNumbers
method returns the product of two integers.
If/Else Statements in Apex:
Conditional statements are crucial for controlling the flow of a program. Apex supports standard if/else statements for decision-making. Here's a breakdown:
If Statement:
public class MyClass {
public void checkNumber(Integer num) {
if (num > 0) {
System.debug('Number is positive');
}
}
}
In this example, if the num
parameter is greater than 0, a debug statement is executed.
If/Else Statement:
public class MyClass {
public void checkNumber(Integer num) {
if (num > 0) {
System.debug('Number is positive');
} else {
System.debug('Number is non-positive');
}
}
}
This includes an additional block of code to be executed when the condition is false.
If/Else If/Else Statement:
public class MyClass {
public void checkNumber(Integer num) {
if (num > 0) {
System.debug('Number is positive');
} else if (num < 0) {
System.debug('Number is negative');
} else {
System.debug('Number is zero');
}
}
}
This structure allows for multiple conditions to be evaluated in a sequential manner.
Multiple If/Else If Statements:
public class MyClass {
public void checkNumberRange(Integer num) {
if (num > 0 && num <= 10) {
System.debug('Number is between 1 and 10');
} else if (num > 10 && num <= 20) {
System.debug('Number is between 11 and 20');
} else if (num > 20) {
System.debug('Number is greater than 20');
} else {
System.debug('Number is non-positive');
}
}
}
In this example, the method checkNumberRange
checks the range of the input number using multiple if/else if
statements.
Nested If Statements:
public class MyClass {
public void checkNumber(Integer num) {
if (num > 0) {
if (num % 2 == 0) {
System.debug('Number is positive and even');
} else {
System.debug('Number is positive and odd');
}
} else if (num < 0) {
System.debug('Number is negative');
} else {
System.debug('Number is zero');
}
}
}
This example demonstrates a nested if
statement. If the number is positive, it further checks whether it's even or odd.