Difference between static and global variables

884 views Asked by At

What is the difference between the code below?

@implementation MyClass

static int myVar =0;
int _myVar =0;

I am getting same values for different objects of MyClass and both are visible to the all methods of MyClass...

3

There are 3 answers

1
Jaffer Wilson On BEST ANSWER

Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).

Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be accessible only in this file (file scope).

On the other hand global variables have to be defined globally, persists (life is) throughout the program, scope is also throughout the program. This means such variables can be accessed from any function, any file of the program.

So if you have a global variable and u r distributing ur files as a library and you want others to not access your global variable, you may make it static by just prefixing keyword static (of course if same variable is not required in other files of yours).

0
tailec On

static limits the scope of your variable. In your case variable will be visible within MyClass file.

However, declaration of variable without static means that variable is automatic by default. It means that your static will live throughout all method calls and automatic will be allocated when you call a method and then at some point deallocated.

0
Droppy On

Neither of those variables are related to MyClass and both are global, which is why you are seeing the same value in all instances of MyClass. If you wanted to make _myVar an instance variable then it needs to go between brackets:

@implementation MyClass {
    int _myVar;
}
...
@end

The difference between the variables in your code is that the static variable cannot be accessed outside the scope of the implementation file (which I assume is called MyClass.m), while the non-static one can be accessed from anywhere in the application, however you'd need to keep the compiler happy with an extern int _myVar; in any code that wants to access it; this is normally done by putting that extern declaration in a header file.