I've seen a few posts discussing what a static variable is and I think I get it - but I'd love to quickly write (or find) a program that utilizes both a regular and a static variable, side by side, and see how/when they operate differently. Some quick n dirty code, maybe two int vars and a couple of NSLog tracking statements just to see HOW they're different.
Anybody got any tips/ideas/code out there that would illustrate how a static var differs from a regular one?
Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.
A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.
Syntax and Use of the Static Variable in C One can define a static variable both- outside or inside the function. These are local to the block, and their default value is always zero. The static variables stay alive till the program gets executed in the end.
static works mostly like in C.
It can either initialize a variable only once.
Declaring a variable static in a file above @implementation
block will be available for the whole file only.
In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program. This is in contrast to automatic variables, whose lifetime exists during a single function call; and dynamically-allocated variables like objects, which can be released from memory when no longer used. More simply put, a static variable's value is maintained throughout all function/method calls. When declared outside of a function, a static variable is visible to everything within the file in which it is declared; when declared inside a function or method, it is visible only within that function or method, but the value is retained between calls.
Say you have this:
int f(void) { int i = 5; i += 10; return i; }
Every call to f()
will return the value 15
.
Now say you have this:
int g(void) { static int i = 5; i += 10; return i; }
The first time g()
is called, the value 15
will be returned. The second time, 25
will be returned, as i
maintained its value of 15
and then incremented itself by 10
. The third call, 35
will be returned. And so on.
In the context of Objective-C classes, static variables are often used to mimic class variables, as Objective-C does not have class variables (other languages, such as Java, do). For instance, say you want to lazily initialize an object, and only return that object. You might see this:
static MyObject *obj = nil; @implementation MyObject + (id)sharedObject { if (obj == nil) obj = [[MyObject alloc] init]; return obj; } @end
obj
will be initialized the first time classObject
is called; subsequent invocations of classObject
will return the same object. You could check this by logging the address of the object:
NSLog(@"obj is at %p", [MyObject sharedObject]); NSLog(@"obj is at %p", [MyObject sharedObject]); // Will print the same address both times
Furthermore, obj
will be visible to all methods in MyObject
.
This technique is used to implemented singleton classes in Objective-C as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With