Keyword

# Static

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Creates a local variable or local array with the name and size (in the case of an array) and data type specified. A variable declared with the <span class="title-ref">Static</span> statement and assigned a value retains its value from one invocation of the method to the next. In contrast, variables declared with the `Var</api/language/var>` statement are completely local to the method and are destroyed when each invocation of the method goes out of scope.

## Usage

The <span class="title-ref">Static</span> statement has two syntaxes:

``` xojo
Static variableName [ , variableNameN ] As [ New ] DataType [ = InitialValue ]
```

| Part          | Type                    | Description                                                                                                                                                                                  |
|---------------|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| variableName  | Variable name           | The name of the new variable.                                                                                                                                                                |
| variableNameN | Variable name           | Optional. The names of other variables you wish to create with the same data type.                                                                                                           |
| DataType      | Data type name          | The data type of the variable.                                                                                                                                                               |
| InitialValue  | Same type as *DataType* | Initial value for *variableName*. If the datatype is an object that requires instantiation via the `New</api/language/new>` operator, you can do that instead of assigning an initial value. |

**or**

``` xojo
Static arrayName(size [ , sizeN ]) As DataType
```

| Part      | Type                               | Description                                                                                          |
|-----------|------------------------------------|------------------------------------------------------------------------------------------------------|
| arrayName | Variable name                      | The name of the new array.                                                                           |
| size      | `Integer</api/data_types/integer>` | The size (number of elements) for the array. Arrays are zero-based.                                  |
| sizeN     | `Integer</api/data_types/integer>` | Optional. The size of the next dimension of the array if you are creating a multi-dimensional array. |
| DataType  | Data type name                     | The data type of the array.                                                                          |

## Notes

A <span class="title-ref">Static</span> variable is equivalent to a local variable declared by the `Var</api/language/var>` statement but its value is retained between several calls of the method it is declared in. Also, all the instances of a given class share the same <span class="title-ref">Static</span> values.

<div class="note">

<div class="title">

Note

</div>

Whenever you assign an initial value to a <span class="title-ref">Static</span> variable using the equal sign, the assignment will occur only once when the method is called for the very first time.

</div>

For example, if you want a serial number to be generated, i.e. a method with always returns a new increasing value, the simplest is to use a <span class="title-ref">Static</span> value as:

``` xojo
Function SerialNumber() As Integer
  Static currentSerialNumber As Integer = 0

  currentSerialNumber = currentSerialNumber + 1 ' Increment the value
  Return currentSerialNumber ' Return the value
End Function
```

You can also use <span class="title-ref">Static</span> to cache a value which takes time to get so you can improve the performance of your code:

``` xojo
Computed Property MyCachedProperty
Get
  ' Inside the Get part
  Static theProperty As String = AMethodWhichTakesSomeTime ' This will be executed only the first time

  Return theProperty
End Get
```

## Sample code

This example uses the <span class="title-ref">Static</span> statement to create an `Integer</api/data_types/integer>` variable called "age" and assigns it the value 33.

``` xojo
Static age As Integer = 33
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This example uses the <span class="title-ref">Static</span> statement to create two `string</api/data_types/string>` variables.

``` xojo
Static FirstName, LastName As String
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This example uses the <span class="title-ref">Static</span> statement to create an array called aNames with 11 elements (remember, arrays have a zero element).

``` xojo
Static aNames(10) As String
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This example uses the <span class="title-ref">Static</span> statement to create an array called aNames with one element.

``` xojo
Static aNames(0) As String
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Arrays</api/language/arrays>` concept; `Collection</api/language/collection>`, `Dictionary</api/language/dictionary>` classes; `Var</api/language/var>` statement.

</div>
