Static Confusion for me

Sample code

This example uses the Static statement to create an Integer variable called “age” and assigns it the value 33.

Static age As Integer
age = 33

The above is from the Xojo Documentation. I find this confusing . If these two lines of code occur in a method, how is this functionally different than

Var age As Integer
age = 33

?

It seems to me that although the value of age might be retained between different invocations of the method, after these two lines of code whether you are using Var or Static effectively the result is exactly the same. Perhaps later on in the method, the value of age is changed and that is “remembered” if declared age is declared as Static, but it still ends up as 33 when the method is called again. So when I see this Sample code in the documentation what exactly is it trying to demonstrate.? Why would you ever be declaring as Static?


Static age As Integer
age = 33

This is functionally (but not internally) identical to

Var age As Integer
age = 33

because the assignment occurs on a second line.

Whereas

Var age As Integer = 21

if age = 21 then
//have a drink
age = 22
end if

in this example, its 21 first time through.
Second time through, it SHOULD have a value of 22 because the variable has not been cleared.

I usually use statics like this:

Var dunnit As boolean = false

if not dunnit then
'do it now, one time only
dunnit= true
end if

It becomes more interesting when the static variable is part of a class definition. The value is then shared between all instances of the class. This can be either very powerful or very error producing, depending on how you use it.

In general, static is probably much more limited use than a simple var.

3 Likes

I presume you mean

Static dunnit As Boolean = False

That makes sense to me. And in the example in the documentation if it had been,

Static age As Integer = 33

that would also makes sense. But the example

Static age As Integer
age = 33

makes no sense to me.

1 Like

Yeah. Just woke up… :slight_smile:

The documentation page is confusing, and this isn’t the only error on that page.

If understand the topic correctly, the documentation should have stressed that

Var count As Integer = 30

and

Var count As Integer
count = 30

are exactly the same thing. The first version might be considered a typing shortcut.

However:

Static count As Integer = 30

and

Static count As Integer
count = 30

are two different animals and have different consequences when they appear in a method.

IMO, the documentation obscures this fact which as I see it is central to an understanding of Static. But I have never actually used Static in my code, although now I can see that it might have been valuable in circumstances that I used an alternate strategy.

I also sense, as Tim mentions, that you might find yourself struggling to understand a bug in your code arising from Static crossing from one instance of a class to another instance of that class. Even to a “future” instance.

It caught me out once, for sure. I assumed it was ‘static for this instance’, and it turned out to be ‘static for all instances, even when other properties varied’

Statis variables aren’t meant to keep their value between to run of the Method/Function they’re in ?

So, if you initialize it at 30, and in the Method you add 1: it will be 31 on the next Method run, 32 on the folowwing one, etc.

Or I must take a nap right now and do some gardening ?

One really good use of a static is when the return value of the method is of high cost and won’t change. So for instance, if you had a method which called out to a console app to get its value, you could do this:

Static uname as String
If uname = "" then
    Var sh as new Shell
    Sh.execute("uname")
    Uname = sh.result.trim()
End if

Return uname

In this way, the shell would only ever be created and called the first time it was used.

1 Like

It’s a poor example. Static can be useful in many situations, e.g. recursive functions, but I agree it seems pretty pointless to assign a value to it as shown.

A dynamic variable is allocated at its declaration place and its value attributed at that time, EVERY time the program runs over such line, it starts over. Once out of scope, that memory is disposed.

A static variable is allocated at load time of static structures, once; and its initialization is done once too, at its declaration place. And is not disposed, it stays there even if its declaration, use, and scope ends. Once the code runs again over the code where it was declared at its first time, is does nothing, its already there, but those next lines using it will use what’s there, stored from past iterations.

I think the OP understood that, he was just questioning the silly example in the documentation which defeats the purpose of using a Static variable, as it assigns a fixed value to it.

3 Likes

Another good use of Static is reusable objects, like a RegEx. I will often use code like this:

Function ScanTextOrSomething(s As String) As Boolean
  Static rx As RegEx

  If rx Is Nil Then
    rx = New RegEx
    rx.SearchPattern = "my pattern"
    rx.Options = <set options
  End If

  // Now use the RegEx that never has to be created again
End Function
1 Like

I thought that this code was him not understanding how it works.

:slightly_smiling_face: :flushed:

Julia is correct. When I saw the Sample Code, I got confused and thought perhaps there was some magical way that if a value was assigned immediately (next line) after the variable was declared as a Static then it would only pertain the first time through the method. When I experimented, this was not the case. And then I started to wonder what the rational of the Sample Code was.

The discussion has made it clear to me that the Sample Code is, in fact, ridiculous. And I have also learned about more suitable circumstances/methodologies for the use of Static.

Appreciated.

3 Likes

I wrote a blog post about static: Static Variables in Xojo

1 Like

This statement is incorrect:

“Just swap your “var” or “dim” with a “static” and you get a global variable.”

The visibility scope is still preserved. You can’t say Static makes Globals, you must explain the intent differently.

Static makes variables “non-volatile”. Exists at a fixed place in the memory during all the app lifetime.

2 Likes