Apex Class
Apex Class
Developers
Apex Developer Guide / Writing Apex / Classes, Objects, and Interfaces / Classes / Class Methods
Class Methods
Class Methods
To define a method, specify the following:
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.
Note
Note
You can use override to override methods only in classes that have been defined as
virtual or abstract .
For example:
As in Java, methods that return values can also be run as a statement if their results aren’t assigned
to another variable.
User-defined methods:
Can be recursive.
Can have side effects, such as DML insert statements that initialize sObject record IDs. See
Apex DML Statements.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm 1/4
4/24/25, 3:08 PM Class Methods | Apex Developer Guide | Salesforce Developers
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 aren’t needed.
Can be overloaded. 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 can’t find an exact match, it then seeks
an approximate match using type coercion rules. For more information on data conversion,
see Rules of Conversion.
Note
Methods with a void return type are typically invoked as a standalone statement in Apex
code. For example:
Can have statements where the return values are run as a statement if their results aren’t
assigned to another variable. This rule is the same in Java.
Note
All user-defined types support the clone method. The clone() method in Apex is based
on the clone method in Java.
Non-primitive data type arguments, such as sObjects, are passed into methods by reference.
Therefore, when the method returns, the passed-in argument still references the same object as
before the method call. Within the method, the reference can't be changed to point to another
object but the values of the object's fields can be changed.
The following are examples of passing primitive and non-primitive data type arguments into
methods.
This example shows how a primitive argument of type String is passed by value into another
method. The debugStatusMessage method in this example creates a String variable, msg, and assigns
it a value. It then passes this variable as an argument to another method, which modifies the value
of this String. However, since String is a primitive type, it’s passed by value, and when the method
returns, the value of the original variable, msg, is unchanged. An assert statement verifies that the
value of msg is still the old value.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm 2/4
4/24/25, 3:08 PM Class Methods | Apex Developer Guide | Salesforce Developers
// change; it is still the old value.
System.assertEquals(msg, 'Original value');
}
This example shows how a List argument is passed by reference into the reference() method and
is modified. It then shows, in the referenceNew() method, that the List argument can't be changed
to point to another List object.
First, the createTemperatureHistory method creates a variable, fillMe, that is a List of Integers and
passes it to a method. The called method fills this list with Integer values representing rounded
temperature values. When the method returns, an assert statement verifies that the contents of the
original List variable has changed and now contains five values. Next, the example creates a second
List variable, createMe, and passes it to another method. The called method assigns the passed-in
argument to a newly created List that contains new Integer values. When the method returns, the
original createMe variable doesn't point to the new List but still points to the original List, which is
empty. An assert statement verifies that createMe contains no values.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm 3/4
4/24/25, 3:08 PM Class Methods | Apex Developer Guide | Salesforce Developers
© Copyright 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce, Inc.
Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
Privacy Information Terms of Use Legal Use of Cookies Trust Cookie Preferences
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm 4/4