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?
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.
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’
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.
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.
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
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.