# About STATIC in Xojo

**URL:** <https://ifnotnil.com/t/about-static-in-xojo/280>\
**Category:** General Programming\
**Created:** [3 April 2020 20:27 UTC](https://ifnotnil.com/t/about-static-in-xojo/280 "2020-04-03T20:27:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [3 April 2020 20:27 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/1 "2020-04-03T20:27:31Z")

</div>

Statics serve a useful purpose  
But, you do have to be aware of how they behave.

Suppose you create a STATIC in a method on a class.  
Be aware that this will exist ONCE for ALL instance of the class (this may or may not be desirable for your purposes)

As a quick example if you do

```auto
Class Class1
     Computed Property Foo as String
          Get as string
              static msg as String
              if msg = "" then
                 break
                 msg = "this gets called once"  
              end if
          end get
     End Property
End Class

```

and then in other code do

```auto

Dim c1 As New class1

Dim u1 As String = c1. Foo

Dim c2 As New Class1

Dim u2 As String = c2. Foo

break

```

note how many time the break statement is reached in the Getter regardless of how many instances you create

with STATIC there is ONE instance across ALL instances - much like a shared property behaves

so heads up !

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [3 April 2020 21:05 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/2 "2020-04-03T21:05:28Z")

</div>

OUCH!

Thanks - I just discovered a major bug in my app that I didn’t even suspect was there.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [3 April 2020 21:56 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/3 "2020-04-03T21:56:08Z")

</div>

Yeah static can be kind of surprising in this way  
There are a few gotchas I’ve come across over the years like this 🙂

---

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [4 April 2020 06:06 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/4 "2020-04-04T06:06:36Z")

</div>

Nice illustration @npalardy. Like @MarkusWinter, I need to now fix some broken code…

---

<div class="post-metadata">

**Author:** ![s7g2vp2](https://ifnotnil.com/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@s7g2vp2](https://ifnotnil.com/u/s7g2vp2)\
**Post date:** [4 April 2020 12:18 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/5 "2020-04-04T12:18:14Z")

</div>

STATIC can also cause weird multi-threading bugs.  
For example, if multiple threads called a function that read / write a static value they could end up treading on each other’s toes. Granted, this is harder with Xojo’s co-operative threading but could still happen if a yield occurred at the wrong time.

---

<div class="post-metadata">

**Author:** ![Rick.A](https://ifnotnil.com/user_avatar/ifnotnil.com/rick.a/32/217_2.png) [@Rick.A](https://ifnotnil.com/u/Rick.A)\
**Post date:** [4 April 2020 19:05 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/6 "2020-04-04T19:05:31Z")

</div>

> [@s7g2vp2](#):
>
> if multiple threads called a function that read / write a static value…

The sync methods exists to control such things… [CriticalSection](https://docs.xojo.com/CriticalSection), [Semaphore](https://docs.xojo.com/Semaphore), [Mutex](https://docs.xojo.com/Mutex)

---

<div class="post-metadata">

**Author:** ![s7g2vp2](https://ifnotnil.com/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@s7g2vp2](https://ifnotnil.com/u/s7g2vp2)\
**Post date:** [5 April 2020 10:52 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/7 "2020-04-05T10:52:33Z")

</div>

They do exist but they have to be used wisely otherwise you lose the benefit of the optimisations that STATIC would provide.

It also means that you have to design functions taking into consideration that this could happen which I imagine the majority of users wouldn’t.

---

<div class="post-metadata">

**Author:** ![Rick.A](https://ifnotnil.com/user_avatar/ifnotnil.com/rick.a/32/217_2.png) [@Rick.A](https://ifnotnil.com/u/Rick.A)\
**Post date:** [5 April 2020 11:13 UTC](https://ifnotnil.com/t/about-static-in-xojo/280/8 "2020-04-05T11:13:17Z")

</div>

> [@s7g2vp2](#):
>
> you have to design functions taking into consideration that this could happen

Of course. That’s why programming is not the same as selling bananas. 😃  
Threading is not for everyone. It’s in the “Advanced topics” in any language. 😉
