Newer Version Available
Class Methods
- Optional: Modifiers, such as public or protected.
- Required: The data type of the value returned by the method, such as String or Integer. Use void if the method does not return a value.
- Required: A list of input parameters for the method, separated by commas, each preceded by its data type, and enclosed in parentheses (). If there are no parameters, use a set of empty parentheses. A method can only have 32 input parameters.
- Required: The body of the method, enclosed in braces {}. All the code for the method, including any local variable declarations, is contained here.
1[public | private | protected | global] [override] [static] data_type method_name
2(input parameters)
3{
4// The body of the method
5}1public static Integer getInt() {
2 return MY_INT;
3 }As in Java, methods that return values can also be run as a statement if their results are not assigned to another variable.
- Can be used anywhere that system methods are used.
- Can be recursive.
- Can have side effects, such as DML insert statements that initialize sObject record IDs. See Apex DML Statements.
- Can refer to themselves or to methods defined later in the same class or anonymous block. Apex parses methods in two phases, so forward declarations are not needed.
- Can be polymorphic. For example, a method named example can be implemented in two ways, one with a single Integer parameter and one with two Integer parameters. Depending on whether the method is called with one or two Integers, the Apex parser selects the appropriate implementation to execute. If the parser cannot find an exact match, it then seeks an approximate match using type coercion rules. For more information on data conversion, see Rules of Conversion.
- When using void methods that have side effects, user-defined methods are
typically executed as stand-alone procedure statements in Apex code. For
example:
1System.debug('Here is a note for the log.'); - Can have statements where the return values are run as a statement if their results are not assigned to another variable. This rule is the same in Java.
Passing Method Arguments by Value
In Apex, all primitive data type arguments, such as Integer or String, are passed into methods by value. This fact means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.
Non-primitive data type arguments, such as sObjects, are passed into methods by reference. This means that when the method returns, the passed-in argument still references the same object as before the method call and can't be changed to point to another object. However, the values of the object's fields can be changed in the method.
The following are examples of passing primitive and non-primitive data type arguments into methods.
Example: Passing Primitive Data Type Arguments
1public class PassPrimitiveTypeExample {
2 public static void debugStatusMessage() {
3 String msg = 'Original value';
4 processString(msg);
5 // The value of the msg variable didn't
6 // change; it is still the old value.
7 System.assertEquals(msg, 'Original value');
8 }
9
10 public static void processString(String s) {
11 s = 'Modified value';
12 }
13}Example: Passing Non-Primitive Data Type Arguments
1public class PassNonPrimitiveTypeExample {
2
3 public static void createTemperatureHistory() {
4 List<Integer> fillMe = new List<Integer>();
5 reference(fillMe);
6 // The list is modified and contains five items
7 // as expected.
8 System.assertEquals(fillMe.size(),5);
9
10 List<Integer> createMe = new List<Integer>();
11 referenceNew(createMe);
12 // The list is not modified because it still points
13 // to the original list, not the new list
14 // that the method created.
15 System.assertEquals(createMe.size(),0);
16 }
17
18 public static void reference(List<Integer> m) {
19 // Add rounded temperatures for the last five days.
20 m.add(70);
21 m.add(68);
22 m.add(75);
23 m.add(80);
24 m.add(82);
25 }
26
27 public static void referenceNew(List<Integer> m) {
28 // Assign argument to a new List of
29 // five temperature values.
30 m = new List<Integer>{55, 59, 62, 60, 63};
31 }
32}