---
schema_version: "1"
id: a26e251113a4e60f30aa279ff36c7fb555668c29a48a5653e09367f57aacfc7c
title: Class Variables
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_classes_declaring_variables.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_declaring_variables.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 520f118aecab5451846375ecb760008eb3acfdb1d3a6aff18c818c0dad8d1808
---
# Class Variables

To declare a variable, specify the following:

- Optional: Modifiers, such as public or final, as well as static.
- Required: The data type of the variable, such as String or Boolean.
- Required: The name of the variable.
- Optional: The value of the variable.

Use the following syntax when defining a variable, where data\_type, variable\_name, and value are placeholder values:

\[public | private | protected | global\] \[final\] \[static\] data\_type variable\_name 
\[= value\] 

For example:

      private static final Integer MY\_INT; 
      private final Integer i = 1;

## Versioned Behavior Changes

In API version 50.0 and later, scope and accessibility rules are enforced on Apex variables, methods, inner classes, and interfaces that are annotated with @namespaceAccessible. For accessibility considerations, see [NamespaceAccessible Annotation](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_NamespaceAccessible.htm). For more information on namespace-based visibility, see [Namespace-Based Visibility for Apex Classes in Second-Generation Packages](https://developer.salesforce.com/docs/atlas.en-us.264.0.sfdx_dev.meta/sfdx_dev/sfdx_dev_unlocked_namespace_visibility.htm "HTML (New Window)").

- [← Previous](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm "Apex Class Definition")
- [Next →](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm "Class Methods")