---
schema_version: "1"
id: e8c435aa87a035a48e6b30d048d037e6a8d037f02111f8e919bc8fe571dd2baa
title: Define Apex Classes
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/platform/264-0-0/code_define_package.md
canonical_url: https://help.salesforce.com/s/articleView?id=platform.code_define_package.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 84aaf631c54c9fef2c983983235103193c0d1177b249de3f10fbf39025d3bd39
description: Salesforce stores Apex classes as metadata.
products:
  - Salesforce Platform
release: "264"
---
# Define Apex Classes

Salesforce stores Apex classes as metadata.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: Salesforce Classic (<a href="https://help.salesforce.com/s/articleView?id=xcloud.overview_edition_lex_only.htm&amp;type=5&amp;language=en_US" target="_blank">not available in all orgs</a>)</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Performance</strong>, <strong>Unlimited</strong>, <strong>Developer</strong>, <strong>Enterprise</strong>, and <strong>Database.com</strong> Editions</td></tr></tbody></table>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:74%">
          <col style="width:26%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d10461e79">User Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d10461e79 ">
              <span id="code_define_package__class_perm_text"><a name="code_define_package__class_perm_text"><!-- --></a>To define, edit, delete, set security, and set version
                settings for Apex classes:</span>
            </td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d10461e79 ">
              <span id="code_define_package__apex_perm_value"><a name="code_define_package__apex_perm_value"><!-- --></a>Author Apex</span>
            </td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d10461e79 ">
              <span id="code_define_package__class__runtest_perm_text"><a name="code_define_package__class__runtest_perm_text"><!-- --></a>To run Apex tests:</span>
            </td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d10461e79 ">
              <span id="code_define_package__apex_runtest_perm_value"><a name="code_define_package__apex_runtest_perm_value"><!-- --></a>View Setup and Configuration</span>
            </td>

          </tr>
        </tbody>
      </table>

You can add, edit, or delete Apex using the Salesforce user interface only in a Developer Edition organization, a Salesforce Enterprise Edition trial organization, or sandbox organization. In a Salesforce production organization, you can change Apex only by using the Metadata API `deploy` call, the Salesforce Extensions for Visual Studio Code, or the Ant Migration Tool. The Salesforce Extensions for Visual Studio Code and Ant Migration Tool are free resources provided by Salesforce to support its users and partners, but are not considered part of our Services for purposes of the Salesforce Main Services Agreement.

1. From Setup, enter Apex Classes in the Quick Find box, then select **Apex Classes**.
2. Click **New**.
3. Click **Version Settings** to specify the version of Apex and the API used with this class.
    
    If your organization has installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this class. Use the default values for all versions. This associates the class with the most recent version of Apex and the API, as well as each managed package. You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version. You can specify an older version of Apex and the API to maintain specific behavior.
4. In the class editor, enter the Apex code for the class. A single class can be up to 1 million characters in length, not including comments, test methods, or classes defined using `@isTest`.
5. Click **Save** to save your changes and return to the class detail screen, or click **Quick Save** to save your changes and continue editing your class. Your Apex class must compile correctly before you can save your class.

Once saved, classes can be invoked through class methods or variables by other Apex code, such as a trigger.

![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-platform-extend_code-264-0-0-production-enus/789bc523-706b-4ae4-8fe9-be6322e38b6b/images/icon_note.png)

Note To aid backwards-compatibility, classes are stored with the version settings for a specified version of Apex and the API. If the Apex class references components, such as a custom object, in installed managed packages, the version settings for each managed package referenced by the class is saved too. Additionally, classes are stored with an `isValid` flag that is set to `true` as long as dependent metadata has not changed since the class was last compiled. If any changes are made to object names or fields that are used in the class, including superficial changes such as edits to an object or field description, or if changes are made to a class that calls this class, the `isValid` flag is set to `false`. When a trigger or Web service call invokes the class, the code is recompiled and the user is notified if there are any errors. If there are no errors, the `isValid` flag is reset to `true`.

#### See Also

- [Manage Apex Classes](https://help.salesforce.com/s/articleView?id=platform.code_manage_packages.htm&release=264.0.0&type=5 "An Apex class is a template or blueprint from which Apex objects are created. Classes consist of other classes, user-defined methods, variables, exception types, and static initialization code.")
- [View Apex Classes](https://help.salesforce.com/s/articleView?id=platform.code_view_class.htm&release=264.0.0&type=5 "After you have created a class, you can view the code contained in the class, as well as the API against which the class was saved, and whether the class is valid or active.")
- [Manage Version Settings for Apex](https://help.salesforce.com/s/articleView?id=platform.code_version_settings_apex.htm&release=264.0.0&type=5 "To aid backwards-compatibility, Apex classes and triggers are stored with version settings for the Salesforce API and any referenced managed packages.")